Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: if not val, vs if val is None

I've always coded in the style of if not value, however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems:

  1. It's not completely readable; if value is None is surely more understandable.
  2. This can have implications later (and cause subtle bugs), since things like [] and 0 will evaluate to False as well.

I am also starting to apply this idea to other comparisons, such as:

  • if not value vs if value is False
  • if not value vs if value is []

And so goes the list...

The question is, how far do you go with the principle? Where to draw the line, while keeping your code safe?

Should I always use the if value is None style no matter what?

like image 852
John Doe Avatar asked Aug 22 '11 19:08

John Doe


People also ask

Is if not the same as is None Python?

Python is (not) crazy. When you do if val is None , you call the operator is , which checks the identity of x . i.e, if val is value Here, is operator checks whether both the operands refer to the same object or not. None is a singleton in Python and all None values are also the exact same instance.

How check value is None or not in Python?

To check none value in Python, use the is operator. The “is” is a built-in Python operator that checks whether both the operands refer to the same object or not.

What does not None mean in Python?

It's often used as the default value for optional parameters, as in: def sort(key=None): if key is not None: # do something with the argument else: # argument was omitted. If you only used if key: here, then an argument which evaluated to false would not be considered.

What does if None evaluate to in Python?

However, None evaluates to False, so the or operator returns the first "True" value, which is the second value. We have to modify the code so that both the or arguments are True. We do this by putting both arguments inside a list, which will then always evaluate to True.


1 Answers

Use a comparison to None if that's what you want. Use "if not value" if you just want to check if the value is considered false (empty list, none, false).

I find "if not value" to be cleaner looking and Pythonic.

Also, be careful with lists. You should not use is when comparing for an empty list. If you know you're getting a list, use if <list> to check if it has any contents (or len()). Try typing this into the interpreter:

>>> a = [] >>> a is [] False 

This is because the temporary list you just made has a different address in memory than the one stored at 'a'. You don't see this with None, False, or True because these are all values that are singletons (they all refer to the same section of memory) so using the 'is' keyword works.

You'll also find that CPython interns strings so the following works.

>>> 'a' is 'a' True 

You should not rely on this. It is an implementation detail and this is not specified to work with every version of Python.

like image 95
Jonathan Sternberg Avatar answered Sep 21 '22 04:09

Jonathan Sternberg