Python `if x is not None` or `if not x is None`?
The proper way to check if a variable x
is not None
in Python is to use if x is not None
.
if x is not None:
# do something
It is not recommended to use if not x is None
as it is less readable, and the double negation can be confusing.
if not x is None: # not recommended
# do something
Watch a video course
Python - The Practical Guide
It is also common and more pythonic to use if x
instead of if x is not None
if x:
# do something
As None
is considered as a False value in python, The above statement will also check if x is not None