Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 271
David Metcalfe Avatar asked Nov 29 '22 06:11

David Metcalfe


1 Answers

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...
like image 119
Brian Rodriguez Avatar answered Nov 30 '22 21:11

Brian Rodriguez