Should I return None or (None, None)?

We have an object method that returns a city / state tuple, i.e. ('Boston', 'MA') . In some valid circumstances, there is no valid city / state for return. Stylistically, does it make sense to return None or a two-element tuple containing (None, None) in this case?

+54
python coding-style
Aug 16 '11 at 18:14
source share
9 answers

I would return None . If there is no result, why return something similar to the result?

It is also easier to check:

 result = getCity() if result: # do something 

I would only return (None, None) , if possible, that only one of the two values โ€‹โ€‹is None (i.e. ('Boston', None) ). In this case, it will be more consistent.

+56
Aug 16 '11 at 18:16
source share

By returning only one value in exceptional circumstances, you risk breaking the unboxing icon. Some of your subscribers may issue:

 city, state = getCityStateTuple("something") 

In this case, returning None will cause the caller to fail with the error:

TypeError: 'NoneType' object is not iterable

So, I will personally return (None, None) in your situation. Again, your mileage may vary, and it depends on the template used by your subscribers.

+43
Aug 16 '11 at 18:19
source share

As others noted, a tuple with elements in it is not tested as False , which is one of the reasons why you can return None , and not (None, None) . However, you can write a set of subcategories that is checked as False , even if it has elements, overriding its __nonzero__() method.

 class falsetuple(tuple): def __nonzero__(self): return False 

Then you can return falsetuple((None, None)) when there is no value available. In fact, you can always return the same falsetuple .

I donโ€™t necessarily recommend that you do this, in fact I have serious concerns about violating this agreement, Iโ€™m just saying that the veracity of non-empty tuples is not necessarily the reason for not returning the tuple.

+11
Aug 16 '11 at 18:44
source share

(None, None) does not evaluate to False in Python. In addition, creating a tuple requires more work than, well, not creating a tuple. Therefore, I would prefer None .

+10
Aug 16 '11 at 18:17
source share

If your program usually returns a tuple, then the tuple is what it should continue to return. The real choice is to return (None, None) or collect the exception, and we do not have enough information to offer you good advice.

If it were me, and I chose the tuple for the exception, I would go with FalseTuple, which offers everything, and also understand that the calling code (which uses tuple unpacking) can also test

 if city is None: 

to find out if a valid result was obtained. That way, you support retrieving a tuple in all possible return values โ€‹โ€‹and still letting the pythonic idiom ask the object: โ€œDo you rate it as True? (Let's return to completeness again):

 class FalseTuple(tuple): def __nonzero__(self): return False 
+6
Aug 16 '11 at 10:10
source share

why not turn the state into city property? Thus, your function will always return a single value: City or None.

The return (None, None) is bad for all the reasons mentioned in the other answers, and only serves to support the unpacking of the tuple.

No is the best value to return to a state in which a valid city cannot be returned, but a function returning 1 or 2 values โ€‹โ€‹is not so good, again due to unpacking tuples.

+4
Aug 16 '11 at 19:55
source share

For me, returning (None, None) implies that (None, State) or (City, None) will also be valid return values. If so, go with (None, None), otherwise Felix and Brent will provide very good arguments for simply returning None.

+2
Aug 16 '11 at 18:41
source share

I would execute a public method for the returned object, say isValidLocation() , which returns true if location is valid and false if location is not equal.

+1
Aug 16 '11 at 18:17
source share

If you return None, it will be much easier for you to check the return value.

+1
Aug 16 '11 at 18:18
source share



All Articles