In Python, I would like to write a function that has a variable number of return values and that is easily dealt with. Something like
def test(a):
if a > 0:
return True
else:
return False, 123, 'foo'
can be used like
out = test(-5)
but a disadvantage I see here is that the user would have to check if the return argument is a tuple, and act accordingly. The meaning of the return values is not very explicit.
A variant would be to use dictionaries as return values but since I haven't ever seen this in any code, it feels a little hackish.
Are there better ways to organize the code?
True, None, Nonein theTruecase, for example.numpyandmatplotlibjust to cite two of them) in these circumstances always return a tuple/list, even with a single return value. This makes code using the return value simpler. Also, if the user knows that the function will return a 1-element tuple/list he can simply doout, = test(...)(note the comma) to automatically unpack the single value.