Use of True, False, and None as return values in python functions


Answers

Use if foo or if not foo, there is no need for either == or is for that.

For checking against None, is None and is not None are recommended. This allows you to distinguish it from False (or things that evaluate to False, like "" and []).

Whether get_attr should return None would depend on the context. You might have an attribute where the value is None, and you wouldn't be able to do that. I would interpret None as meaning "unset", and a KeyError would mean the key does not exist in the file.

Question

I think that I fully understand this, but I just want to make sure since I keep seeing people say to NEVER EVER test against True, False, or None. They suggest that routines should raise an error rather than return False or None. Anyway, I have many situations where I simply want to know if a flag is set or not so my function returns True or False. There are other situations where I have a function return None if there was no useful result. From my thinking, neither is problematic so long as I realize that I should never use:

if foo == True
if foo == False
if foo == None

and should instead use:

if foo is True
if foo is False
if foo is None

since True, False, and None are all singletons and will always evaluate the way I expect when using "is" rather than "==". Am I wrong here?

Along the same lines, would it be more pythonic to modify the functions that sometimes return None so that they raise an error instead? Say I have an instance method called "get_attr()" that retrieves an attribute from some file. In the case where it finds that the attribute I requested does not exist is it appropriate to return None? Would it be better to have them raise an error and catch it later?




In the examples in PEP 8(Style Guide for Python Code) document, I have seen that foo is None or foo is not None are being used instead of foo == None or foo != None. Also using if boolean_value is recommended in this document instead of if boolean_value == True or if boolean_value is True. So I think if this is the official Python way, we Python guys should go on this way, too.

Regards.




for True, not None:

if foo:

for false, None:

if not foo:



Concerning whether to raise an exception or return None: it depends on the use case. Either can be pythonic. Look at python's dict class for example, x[y] hooks into dict.__getitem__ and it raises a KeyError if key is not present. But dict.get method returns the second argument (which is defaulted to None) if key is not present. They are both useful.

The most important thing to consider is to document that behaviour in the docstring, and make sure that your get_attr() method does what it says it does.

To address your other questions, use these conventions:

if foo:
    # for testing truthiness

if not foo:
    # for testing falsiness

if foo is None:
    # testing .. Noneliness ?

if foo is not None:
    # check explicitly avoids common bugs caused by empty sequences being false

Functions that return True or False should probably have a name that makes this obvious to improve code readability

def is_running_on_windows():
    return os.name == 'nt'

In python3 you can "type-hint" that:

>>> def is_running_on_windows() -> bool:
...     return os.name == 'nt'
... 
>>> is_running_on_windows.__annotations__
{'return': bool}





Links



Tags

python python