Wondering if if not foo is None
is the same as if foo
? Using Python 2.7 and foo
is a string.
-
3@Signal: No they are not. ``if foo is not None`` only checks if foo is really not the singleton ``None`` while ``if foo`` checks ``if bool(foo)`` or ``if foo.__nonzero__()`` which are completly different things. – MSeifert Mar 12 '16 at 00:48
-
@MSeifert, nice post, what does `if bool(foo)` and `if foo.__nonzero__()` means for strings in Python? – Lin Ma Mar 12 '16 at 03:39
-
1Whenever the ``len`` of a string is more than 0 ``bool(that_string)`` returns ``True``. If the ``len`` of the string is 0 then it returns ``False``. I think for the builtins like str there is no ``__nonzero__``-method, but I'm not sure. – MSeifert Mar 12 '16 at 03:45
-
1Sorry I mixed up the names ... the method is called ``__bool__`` and not ``__nonzero__``. – MSeifert Mar 12 '16 at 03:50
-
@MSeifert, thanks and vote up. Do you mean there is only one method `__bool__` which is called if I use `if foo`? – Lin Ma Mar 12 '16 at 03:52
-
1I've uploaded my test, you can checkout this [gist](https://gist.github.com/MSeifert04/19eb22199a8f035e5272). – MSeifert Mar 12 '16 at 03:57
-
@MSeifert, nice sample, I am wondering for any variable, what does `__bool__` do? I often see people are using `if not X` to check Null or not for any object. – Lin Ma Mar 12 '16 at 04:13
-
1every variable that is not empty (like an empty list/set/dict/string/...) or 0 (integer, float, ...) or simply ``False`` or ``None`` evaluates to ``True`` when used as ``if variable`` or ``if bool(variable)`` – MSeifert Mar 12 '16 at 04:16
-
@MSeifert, a bit lost, do you mean `if None` return `True`? – Lin Ma Mar 12 '16 at 04:18
-
1I've updated my answer to explain it a bit further. – MSeifert Mar 12 '16 at 04:30
-
Possible duplicate of [Most elegant way to check if the string is empty in Python?](https://stackoverflow.com/questions/9573244/most-elegant-way-to-check-if-the-string-is-empty-in-python) – codeforester Mar 02 '19 at 15:22
4 Answers
For empty strings both are different:
foo = ''
if foo:
print 'if foo is True'
will not print anything because it is empty and therefore considered False
but:
if foo is not None:
print 'if not foo is None is True'
will print because foo
is not None!
I Changed it according to PEP8. if foo is not None
is equivalent to your if not foo is None
but more readable and therefore recommended by PEP8.
A bit more about the general principles in Python:
if a is None:
pass
The if
will only be True
if a = None
was explicitly set.
On the other hand:
if a:
pass
has several ways of evaluating when it is True
:
Python tries to call
a.__bool__
and if this is implemented then the returned value is used.- So
None
,False
,0.0
,0
will evaluate toFalse
because their__bool__
method returnsFalse
.
- So
If
a.__bool__
is not implemented then it checks whata.__len__.__bool__
returns.''
,[]
,set()
,dict()
, etc. will evaluate toFalse
because their__len__
method returns0
. Which isFalse
becausebool(0)
isFalse
.
If even
a.__len__
is not implemented then if just returnsTrue
.- so every other objects/function/whatever is just
True
.
- so every other objects/function/whatever is just
See also: truth-value-testing in thy python documentation.

- 145,886
- 38
- 333
- 352
-
1
-
Thanks MSeifert, in Python any variable (could be string object and any other types of objects) should be point to something, correct? Then how could `if X is not None` be False? – Lin Ma Mar 12 '16 at 03:35
-
MSeifert, nice answer. But how do we know for any class, if `__bool__` or `__len__` is defined? – Lin Ma Mar 12 '16 at 04:32
-
1``help(a)`` shows you all information about the instance or just try ``print a.__bool__()`` or ``print a.__len__()``. (or the shortcuts ``bool(a)``, ``len(a)``) and see what raises an Error (then it's not implemented) or if it prints something (then it's implemented) – MSeifert Mar 12 '16 at 04:34
-
Nice idea MSeifert, vote up. I often see people write `if not X` (X could be any customized class types) to check if an object is Null or not, and I do not see if they defined `__bool__` or `len` object? Wondering if this writing style/method is correct? Thanks. – Lin Ma Mar 12 '16 at 04:46
-
1Ah forgot the obvious case: if none of those two methods is defined ``bool(a)`` just returns ``True`` regardless. – MSeifert Mar 12 '16 at 04:53
No, not the same when foo is an empty string.
In [1]: foo = ''
In [2]: if foo:
...: print 1
...:
In [3]: if foo is None:
...: print 2
...:
In [4]: if not foo is None:
...: print 3
...:
3

- 1,859
- 1
- 15
- 24
-
Thanks tianwei, vote up. I often see people write `if not X` (X could be any types of object), if in Python a variable have to point to some object, how could `if not X` be False? Thanks. – Lin Ma Mar 12 '16 at 03:38
-
1@LinMa, when X is neither None nor empty(0,[], {}, () ,or ''), `if not X` would be False. – tianwei Mar 12 '16 at 15:11
Operator is
compares ids (you can check the id of something with id(something)
). As there is only one instance of None
, any expression in the form something is None
will be False
except for None is None
or if something == None
. Operator not
has less precedence than comparisons (and is
is a comparison operator) so the result for not string is None
which is the same as not (string is None)
is always True
.
Nevertheless a string object is considered True
if it is not the empty string and False
if it is the empty string.
So, one of the expressions is always True
and the other can vary so no, they are not equivalent.

- 6,507
- 1
- 26
- 25
-
Thanks Salva, vote up. I often see people write `if not X` (X could be any types of object), if in Python a variable have to point to some object, how could `if not X` be False? Thanks. – Lin Ma Mar 12 '16 at 03:38
-
1And Salva, for `not str is None`, `not` is operate on `str`, or `not` is operated on `str is None`? Thanks. – Lin Ma Mar 12 '16 at 04:22
-
1`if not X` can be `False` because in the context where a boolean is expected such a if-test, `X` is converted into a boolean implicitly. The rules governing such conversions are summarized in the [Python online reference](https://docs.python.org/2/library/stdtypes.html#truth-value-testing). – Salva Mar 12 '16 at 09:55
-
1@LinMa, answering to your second question I realized a failure in the answer reasoning. `not` has less precedence than comparisons so `not str is None` is the same as `not (str is None)`. I edit and fix the answer. – Salva Mar 12 '16 at 10:15
No. Try this code
foo=''
if foo:
print (1)
else:
print (2)
if not foo is None:
print (3)
else:
print (4)
it will print
2
3

- 2,651
- 1
- 10
- 18
-
Thanks Sci, vote up. In Python any variable (could be string object and any other types of objects) should be point to something, correct? Then how could `if X is not None` be False? – Lin Ma Mar 12 '16 at 03:35