0

What return value(s) does Python check when a function returns multiple values, but there is only one 'if' checking the function's result?

Thank you for your time.

def func1(args):
    return pass, data

def func2
   if func1(args):
       ...
   else 
       raise Exception ...
5
  • 1
    You can't return pass from a function. Commented Oct 15, 2021 at 13:44
  • Besides what @yudhiesh correctly stated, when you return multiple values, Python interprets it as a tuple. So it will be True whenever a tuple would evaluate as True. If a tuple has elements, it will evaluate as True. Commented Oct 15, 2021 at 13:45
  • 1
    You aren't actually returning multiple values; you're returning a tuple, which happens to contain two values. A tuple is "truthy" if it has a non-zero number of elements, the values of those elements are not relevant. Commented Oct 15, 2021 at 13:46
  • If you're returning two values then it's a tuple of length 2, which is truthy, because nonempty things are truthy. (NB they don't "evaluate to True" as several people are saying.) Commented Oct 15, 2021 at 13:46
  • @khelwood they do evaluate to True, in boolean context. Commented Oct 15, 2021 at 15:10

2 Answers 2

5

return a, b would return a tuple. The if statement will always evaluate, since non-empty tuples evaluate to True.

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

Comments

3

When you return multiple values, you're actually returning a tuple containing each of those values.

Your if test will return True regardless of the values (even if they are both None)

2 Comments

Thanks. Would the if be true if func1 returned '()', an empty tuple? It does not in the test code I ran.
No. An empty tuple, list, dictionary or string would return False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.