Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning particular output from Python function

Suppose, in MATLAB, I have the following function:

function [sums, diff, prod] = myFun(a, b)  
    sums = a + b;
    diff = a - b;
    prod = a * b;
end

If I just wanted to return, say diff, I’d type in the console [~, diff, ~] = myFun(6, 2) and it returns 4.

Now, if I write a similar thing in Python 3.10 (for purposes of this question, I’m using the Spyder IDE):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def myFun(a, b):
    sums = a + b
    diff = a - b
    prod = a * b
    return sums, diff, prod

I can type sum, diff, prod = myFun(5, 3) and I get the results stored in the workspace. If I only want the output prod from this function, is there a way to do this in a way similar to MATLAB? If not, how would I achieve this?

I would prefer to not store all outputs of the function in a tuple and access a desired element later, but to achieve something as slick as the MATLAB usage for a placeholder output.

>Solution :

Your function returns a tuple containing all three values.

You can unpack that tuple in a number of ways that might meet your requirements:

_, _, prod = myFun(3, 4)
print (prod)
# result: 12

Or you could just extract the third item in the tuple:

prod = myFun(3, 4)[2]
print (prod)
# result: 12

Using _, diff, _ = doesn’t really do anything different from sums, diff, prod =, you’re still taking the three values in the tuple and assigning them to three variable names – it just so happens that one of those variable names is an underscore _, and you reuse it, so if you try to return the value of _ afterwards – it’ll return the value that was assigned to it last.

Just for fun you could take a fairly heavy handed approach to making accessing individual results more convenient by using something like namedtuple:

def myFun(a, b):
    from collections import namedtuple
    result = namedtuple('myFun_return', ['sums', 'diff', 'prod'])
    result.sums = a + b
    result.diff = a - b
    result.prod = a * b
    
    return result

prod = myFun(3, 4).prod
print (prod)
#result: 12

result in this context, would be a namedtuple, and you could access its three attributes – sums, diff, and prod by simply calling them on the result, without having to remember the order or anything. But like I said, that is a fairly hefty approach to something so minor…

Resources:
https://www.pythontutorial.net/python-basics/python-unpacking-tuple/
https://www.w3schools.com/python/python_tuples_unpack.asp

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading