Helene
Helene

Reputation: 959

Handle multiple returns from a function in Python

I wrote a function (testFunction) with four return values in Python:

diff1, diff2, sameCount, vennPlot

where the first 3 values (in the output tuple) were used to plot "vennPlot" inside of the function.

A similar questions was asked : How can I plot output from a function which returns multiple values in Python?, but in my case, I also want to know two additional things:

  1. I will likely to use this function later, and seems like I need to memorize the order of the returns so that I can extract the correct return for downstream work. Am I correct here? If so, is there better ways to refer to the tuple return than do output[1], or output[2]? (output=testFunction(...))

  2. Generally speaking, is it appropriate to have multiple outputs from a function? (E.g. in my case, I could just return the first three values and draw the venn diagram outside of the function.)

Upvotes: 1

Views: 7245

Answers (4)

chepner
chepner

Reputation: 530920

Technically, every function returns exactly one value; that value, however, can be a tuple, a list, or some other type that contains multiple values.

That said, you can return something that uses something other than just the order of values to distinguish them. You can return a dict:

def testFunction(...):
    ...
    return dict(diff1=..., diff2=..., sameCount=..., venn=...)

x = testFunction(...)
print(x['diff1'])

or you can define a named tuple:

ReturnType = collections.namedtuple('ReturnType', 'diff1 diff2 sameCount venn')


def testFunction(...):
    ...
    return ReturnType(diff1=..., diff2=..., sameCount=..., venn=...)

x = testFunction(...)
print(x.diff1)  # or x[0], if you still want to use the index

Upvotes: 5

Jay
Jay

Reputation: 2858

  1. Python supports direct unpacking into variables. So downstream, when you call the function, you can retrieve the return values into separate variables as simply as:

    diff1, diff2, sameCount, vennPlot= testFunction(...)

    EDIT: You can even "swallow" the ones you don't need. For example:

    diff1, *stuff_in_the_middle, vennPlot= testFunction(...)

    in which case stuff_in_the_middle will contain a tuple of 2.

  2. It is quite appropriate AFAIK, even standard library modules return tuples.

    For example - Popen.communicate() from the subprocess module.

Upvotes: 1

Mike Scotty
Mike Scotty

Reputation: 10782

I will likely to use this function later, and seems like I need to memorize the order of the returns so that I can extract the correct return for downstream work. Am I correct here?

It seems you're correct (depends on your use case).

If so, is there better ways to refer to the tuple return than do output[1], or output[2]? (output=testFunction(...))

You could use a namedtuple: docs

or - if order is not important - you could just return a dictionary, so you can acess the values by name.

Generally speaking, is it appropriate to have multiple outputs from a function? (E.g. in my case, I could just return the first three values and draw the venn diagram outside of the function.)

Sure, as long as it's documented, then it's just what the function does and the programmer knows then how to handle the return values.

Upvotes: 1

Jared Andrews
Jared Andrews

Reputation: 272

To answer your first question, you can unpack tuples returned from a function as such:

diff1, diff2, samecount, vennplot = testFunction(...)

Secondly, there is nothing wrong with multiple outputs from a function, though using multiple return statements within the same function is typically best avoided if possible for clarity's sake.

Upvotes: 1

Related Questions