if x:, vs if x == True, vs if x is True
The following values in Python are false in the context of if
and other logical contexts:
False
None
- numeric values equal to 0, such as
0
,0.0
,-0.0
- empty strings:
''
andu''
- empty containers (such as lists, tuples and dictionaries)
- anything that implements
__bool__
(in Python3) to returnFalse
, or__nonzero__
(in Python2) to returnFalse
or0
. - anything that doesn't implement
__bool__
(in Python3) or__nonzero__
(in Python2), but does implement__len__
to return a value equal to 0
An object is considered "false" if any of those applies, and "true" otherwise, regardless of whether it's actually equal to or identical with False
or True
Now, if you've arranged that x
is necessarily one of the objects True
or False
, then you can safely write if x
. If you've arranged that the "trueness" of x
indicates whether or not to perform the operation, regardless of type, then you can safely write if x
. Where you can write that you should prefer to do so, since it's cleaner to read.
Normally, if it is allowed for x
to take the value True
then you're in one of those two cases, and so you would not write if x is True
. The important thing is to correctly document the meaning of x
, so that it reflects the test used in the code.
Python programmers are expected to know what's considered true, so if you just document, "runs the function if x
is true", then that expresses what your original code does. Documenting it, "runs the function if x is True
" would have a different meaning, and is less commonly used precisely because of the style rule in PEP8 that says to test for trueness rather than the specific value True
.
However, if you wanted the code to behave differently in the case where x
is an empty container from the case where it is None
, then you would write something like if x is not None
.
x = 'False'
x = 123
Are both True
Other truth values.
The document explains other values.
As far as the PEP8 reason, its far more semantic to read if this_file_is_green
Other falsey values include 0
, ''
, []
. You should just use the if x:
version.
It goes without saying that you should write code that does what you need. But in most cases, you simply don't need to say == True
or is True
, because you don't need to distinguish True
from other "truthy" values. So it's recommended to leave that out for simplicity.
The case where you definitely should use == True
or is True
is when you do need to distinguish True
from other truthy values.
In your example, do you care about the difference between True
and 123
? That would tell you which way to code it.
One thing about coding == True
or is True
: it will raise a minor red flag when other developers read your code. They won't think it's wrong, they will just wonder why it's there and will want to know why it's important to treat True
differently from other truthy values in this particular case.
In other words, if you don't need it, it's best not to use it.
I'd like to add a short example where those 3 test differ:
def test(x):
print(x, ":", bool(x), x == True, x is True)
test("something")
test(1)
test(True)
The output (pretty formatted):
# "something" : True False False
# 1 : True True False
# True : True True True