Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python `if x is not None` or `if not x is None`? [closed]

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.

like image 836
orokusaki Avatar asked Apr 26 '10 03:04

orokusaki


People also ask

Is None and is not None in Python?

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).

How do you check if a variable is None or not in Python?

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.

Should I use is None or == None Python?

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 ( == ).

Is not None vs if not?

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.


2 Answers

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.

like image 198
Daniel Stutzbach Avatar answered Sep 23 '22 14:09

Daniel Stutzbach


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:

  • Truth Value Testing

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. :]

like image 36
Xavier Ho Avatar answered Sep 25 '22 14:09

Xavier Ho