born_naked
born_naked

Reputation: 798

python function conditional return

When using a conditional return, if you attempt to return more than one value from function, the function behaves erratically with regard to the actual return value.

def test_function(x,y, diagnostic:bool=False):
    w = x*y
    z = []
    if diagnostic: 
        z = [w,w*2]
    return z, w if diagnostic else w

print(test_function(3,4)) # output tuple ([],12)

# lets switch order of the return from z,w to w,z

def test_function(x,y, diagnostic:bool=False):
    w = x*y
    z = []
    if diagnostic: 
        z = [w,w*2]
    return w,z if diagnostic else w

print(test_function(3,4)) # output tuple (12,12) 

# lets try retun the diagnostic value itself to see what function things is happening

def test_function(x,y, diagnostic:bool=False):
    w = x*y
    z = []
    if diagnostic: 
        z = [w,w*2]
    return diagnostic if diagnostic else w

print(test_function(3,4)) # returns 12, so diagnostic is retuning false

# rewrite conditional to "if not"
def test_function(x,y, diagnostic:bool=False):
    w = x*y
    z = []
    if diagnostic: 
        z = [w,w*2]
    return w if not diagnostic else w,z

print(test_function(3,4)) # returns (12, [])

Upvotes: 0

Views: 1709

Answers (2)

born_naked
born_naked

Reputation: 798

If returning multiple values in a conditional return, the values must be explicitly returned as a tuple due to operator precedence:

def test_function(x,y, diagnostic:bool=False):
            w = x*y
            z = []
            if diagnostic: 
                z = [w,w*2]
            return (z, w) if diagnostic else w
          
print(test_function(3,4)) # returns 12
print(test_function(3,4, diagnostic=False)) # returns (12, [12, 24])
w, z = test_function(3,4, diagnostic=True)
print(w) # returns 12
print(z) # returns [12,24]

Upvotes: 0

kaya3
kaya3

Reputation: 51037

The problem is operator precedence: , has lower precedence than ... if ... else ..., so what you actually wrote is like return z, (w if diagnostic else w), or in the second function, it's like return w, (z if diagnostic else w).

The hint for this is that diagnostic is False but you're still returning a pair of values.

For the behaviour you want, you should write return (z, w) if diagnostic else w. Note that the brackets here are not needed to make it a tuple - it is a tuple either way - the brackets are to specify precedence.

Upvotes: 2

Related Questions