0

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, []) 

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.