I've always thought of the if not x is None
version to be more clear, but Google's style guide and PEP-8 both use if x is not None
. Are there any minor performance differences (I'm assuming not), and is there any case where one really doesn't fit (making the other a clear winner for my convention)?*
*I'm referring to any singleton, rather than just None
.
...to compare singletons like None. Use is or is not.
Use the is not operator to check if a variable is not None in Python, e.g. if my_var is not None: . The is not operator returns True if the values on the left-hand and right-hand sides don't point to the same object (same location in memory).
Use the isinstance() Function to Check if a Variable Is None in Python. The isinstance() function can check whether an object belongs to a certain type or not. We can check if a variable is None by checking with type(None) . It returns a tuple, whose first element is the variable whose value we want to check.
Introduction to the Python None valueIt's a good practice to use the is or is not operator to compare a value with None . Note that you cannot override the is operator behavior like you do with the equality operator ( == ).
Both "if x" and "if x is none" are not the same. If you run the statement "if x:" checks whether x is assigned or not. If you execute the statement "if x is not none", is operator checks whether x is none or not. If you are still confused, Assign the values x = none, x = [], and x = 21 and run the following code.
There's no performance difference, as they compile to the same bytecode:
>>> import dis >>> dis.dis("not x is None") 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 RETURN_VALUE >>> dis.dis("x is not None") 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 RETURN_VALUE
Stylistically, I try to avoid not x is y
, a human reader might misunderstand it as (not x) is y
. If I write x is not y
then there is no ambiguity.
Both Google and Python's style guide is the best practice:
if x is not None: # Do something about x
Using not x
can cause unwanted results.
See below:
>>> x = 1 >>> not x False >>> x = [1] >>> not x False >>> x = 0 >>> not x True >>> x = [0] # You don't want to fall in this one. >>> not x False
You may be interested to see what literals are evaluated to True
or False
in Python:
Edit for comment below:
I just did some more testing. not x is None
doesn't negate x
first and then compared to None
. In fact, it seems the is
operator has a higher precedence when used that way:
>>> x [0] >>> not x is None True >>> not (x is None) True >>> (not x) is None False
Therefore, not x is None
is just, in my honest opinion, best avoided.
More edit:
I just did more testing and can confirm that bukzor's comment is correct. (At least, I wasn't able to prove it otherwise.)
This means if x is not None
has the exact result as if not x is None
. I stand corrected. Thanks bukzor.
However, my answer still stands: Use the conventional if x is not None
. :]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With