Returning None or a tuple and unpacking Returning None or a tuple and unpacking python python

Returning None or a tuple and unpacking


Well, you could do...

first,second = foo(True) or (None,None)first,second = foo(False) or (None,None)

but as far as I know there's no simpler way to expand None to fill in the entirety of a tuple.


I don't see what is wrong with returning (None,None). It is much cleaner than the solutions suggested here which involve far more changes in your code.

It also doesn't make sense that you want None to automagically be split into 2 variables.


I think there is a problem of abstraction.

A function should maintain some level of abstraction, that helps in reducing complexity of the code.
In this case, either the function is not maintaining the right abstraction, either the caller is not respecting it.

The function could have been something like get_point2d(); in this case, the level of the abstraction is on the tuple, and therefore returning None would be a good way to signal some particular case (e.g. non-existing entity). The error in this case would be to expect two items, while actually the only thing you know is that the function returns one object (with information related to a 2d point).

But it could also have been something like get_two_values_from_db(); in this case the abstraction would be broken by returning None, because the function (as the name suggest) should return two values and not one!

Either way, the main goal of using a function - reducing complexity - is, at least partially, lost.

Note that this issue would not appear clearly with the original name; that's also why it is always important to give good names to function and methods.