David Metcalfe
David Metcalfe

Reputation: 2411

Python return multiple values and check for return False

I see plenty of good advice about how to return multiple values in a function, but what's the preferred way to also handle checking other returns like False?

For example:

def f():
    if condition1:
        return False
    else:
        return x, y, z

x, y, z = f()

I can verify if [x, y, z] is not None: but how about also checking for False? Is it just if [x, y, z] is not None and f() is not False: or is there a superior way?

Upvotes: 5

Views: 5862

Answers (3)

SethMMorton
SethMMorton

Reputation: 48735

If you are in the unfortunate situation where you must deal with a function that behaves like the one you presented, a clear way to handle it is with a try: statement.

try:
    x, y, z = f()
except TypeError:
    <handle the situation where False was returned>

This works because trying to unpack False raises a TypeError.


If you can modify the function, I might argue that the idiomatic strategy would be to raise an error (either built-in or custom) instead of returning False.

def f():
    if condition1:
        raise ValueError('Calling f() is invalid because condition1 evaluates to True')
    return x, y, z

try:
    x, y, z = f()
except ValueError:
    <handle the situation where a tuple could not be returned>

This has the benefit of showing the true nature of the error in an uncaught traceback rather than the less obvious TypeError: 'bool' object is not iterable. It also has the benefit of giving consistent return types, so there is no confusion from the user.

Documentation becomes much clearer as well, because instead of

"in the case of success it will return a tuple of three elements, but in the case of failure it will return False (not as a tuple)"

the documentation becomes

"returns a tuple of three elements; raises a ValueError on failure"

Upvotes: 9

DYZ
DYZ

Reputation: 57033

Assign the result to a single variable and check if it's false:

retval = f()
if retval != False:
  x,y,z = retval
else: # False
  ...

Upvotes: 2

Brian Rodriguez
Brian Rodriguez

Reputation: 4359

I think it'd help to have more consistency:

def f():
    if condition1:
        return False, None
    else:
        return True, (x, y, z)

success, tup = f()
if success:
    x, y, z = tup
    # use x, y, z...

Upvotes: 12

Related Questions