I am working on a function that usually returns 1 value, but sometimes returns 2 for similar reasons to this post, and noticed some unexpected behavior best illustrated by this example:
def testfcn1(return_two=False):
a = 10
return a, a*2 if return_two else a
def testfcn2(return_two=False):
a = 10
if return_two:
return a, a*2
return a
I would expect both functions to behave the same way. testfcn2 works as expected:
testfcn2(False)
10
testfcn2(True)
(10, 20)
However, testfcn1 always returns two values, and just returns the first value twice if return_two is False:
testfcn1(False)
(10, 10)
testfcn1(True)
(10, 20)
Is there a rationale for this kind of behavior?
lenmethod. A possible exception to this pattern is returningNoneinstead of a tuple, but even then you can return an empty tuple.