[python] if A vs if A is not None:

Can I use:

if A:

instead of

if A is not None:

The latter seems so verbose. Is there a difference?

This question is related to python

The answer is


Most guides I've seen suggest that you should use

if A:

unless you have a reason to be more specific.

There are some slight differences. There are values other than None that return False, for example empty lists, or 0, so have a think about what it is you're really testing for.


They do very different things.

The below checks if A has anything except the values False, [], None, '' and 0. It checks the value of A.

if A:

The below checks if A is a different object than None. It checks and compares the reference (memory address) of A and None.

if A is not None:

UPDATE: Further explanation

Many times the two seem to do the same thing so a lot of people use them interchangeably. The reason the two give the same results is many times by pure coincidence due to optimizations of the interpreter/compiler like interning or something else.

With those optimizations in mind, integers and strings of the same value end up using the same memory space. That probably explains why two separate strings act as if they are the same.

> a = 'test'
> b = 'test'
> a is b
True
> a == b
True

Other things don't behave the same though..

> a = []
> b = []
> a is b
False
> a == b
True

The two lists clearly have their own memory. Surprisingly tuples behave like strings.

> a = ()
> b = ()
> a is b
True
> a == b
True

Probably this is because tuples are guaranteed to not change, thus it makes sense to reuse the same memory.

This shows that you should be extra vigilant on what comparison operator you use. Use is and == depending on what you really want to check. These things can be hard to debug since is reads like prose that we often just skim over it.


if A: will prove false if A is 0, False, empty string, empty list or None, which can lead to undesired results.


It depends on the context.

I use if A: when I'm expecting A to be some sort of collection, and I only want to execute the block if the collection isn't empty. This allows the caller to pass any well-behaved collection, empty or not, and have it do what I expect. It also allows None and False to suppress execution of the block, which is occasionally convenient to calling code.

OTOH, if I expect A to be some completely arbitrary object but it could have been defaulted to None, then I always use if A is not None, as calling code could have deliberately passed a reference to an empty collection, empty string, or a 0-valued numeric type, or boolean False, or some class instance that happens to be false in boolean context.

And on the other other hand, if I expect A to be some more-specific thing (e.g. instance of a class I'm going to call methods of), but it could have been defaulted to None, and I consider default boolean conversion to be a property of the class I don't mind enforcing on all subclasses, then I'll just use if A: to save my fingers the terrible burden of typing an extra 12 characters.


python >= 2.6,

if we write such as

if A:

will generate warning as,

FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.

So we can use

if A is not None:

None is a special value in Python which usually designates an uninitialized variable. To test whether A does not have this particular value you use:

if A is not None

Falsey values are a special class of objects in Python (e.g. false, []). To test whether A is falsey use:

if not A

Thus, the two expressions are not the same And you'd better not treat them as synonyms.


P.S. None is also falsey, so the first expression implies the second. But the second covers other falsey values besides None. Now... if you can be sure that you can't have other falsey values besides None in A, then you can replace the first expression with the second.


The other answers have all given good information, but I feel this needs to be made clear.

No, you should never use:

if A:

if what you need to test is:

if A is not None:

The first will often work as a replacement for the second but the first is a common source of hard to find bugs. Even if all you are doing is writing some quick throw-away code, you should not allow yourself to get into the habit of writing buggy code. Train your fingers and your mind to write and read the correct verbose form of the test:

if A is not None: 

The common use of None is to define optional parameter values and to give a variable a default starting value which means "no value yet", and functions a meaning of "no value retured". If you write a function such as:

def my_func(a_list, obj=None):
    if obj:          # Trying to test if obj was passed
        a_list.append(obj)
    # do something with a_list

This will work fine for many real world uses like:

my_func(user_names, "bob")

But if any of this happens:

my_func(user_names, "")
my_func(user_names, [])
my_func(user_names, 0)

Then the zero-length objects will not be added to the list. When the first code was written, you might know that zero-length user names are not allowed. So the short code works fine. But then you try to modify the code to use an empty string to mean something like an anonymous user with no name, and suddenly this function stops doing what it is expected to do (adding the annonomous user to the list).

For example, lets say you have logic to define when a user is allowed to log in written like:

new_user = get_user_name()

if user_list_ok(my_func(current_users, new_user)):
    # user_list_ok() tests that no more than 10 users can
    # log in at the same time, and that no sigle user can
    # can log in more than 3 times at once.

    # This user is allowed to log in!

    log_user_in(new_user)

This now creates subtle and complex bugs in your code. The anonmous user of "" will not be added to the list for the test, so when the system has hit the limit of 10 users, the test will still allow the anonomous user to log in, pushing the user count to 11. This then might trigger a bug in another part of the system that requires there only be 10 users max.

When you need to test for "var does not have a value" you should always use the longer "is not None" test even though it feels ugly and verbose.


I created a file called test.py and ran it on the interpreter. You may change what you want to, to test for sure how things is going on behind the scenes.

import dis

def func1():

    matchesIterator = None

    if matchesIterator:

        print( "On if." );

def func2():

    matchesIterator = None

    if matchesIterator is not None:

        print( "On if." );

print( "\nFunction 1" );
dis.dis(func1)

print( "\nFunction 2" );
dis.dis(func2)

This is the assembler difference:

Source:

>>> import importlib
>>> reload( test )

Function 1
  6           0 LOAD_CONST               0 (None)
              3 STORE_FAST               0 (matchesIterator)

  8           6 LOAD_FAST                0 (matchesIterator)
              9 POP_JUMP_IF_FALSE       20

 10          12 LOAD_CONST               1 ('On if.')
             15 PRINT_ITEM
             16 PRINT_NEWLINE
             17 JUMP_FORWARD             0 (to 20)
        >>   20 LOAD_CONST               0 (None)
             23 RETURN_VALUE

Function 2
 14           0 LOAD_CONST               0 (None)
              3 STORE_FAST               0 (matchesIterator)

 16           6 LOAD_FAST                0 (matchesIterator)
              9 LOAD_CONST               0 (None)
             12 COMPARE_OP               9 (is not)
             15 POP_JUMP_IF_FALSE       26

 18          18 LOAD_CONST               1 ('On if.')
             21 PRINT_ITEM
             22 PRINT_NEWLINE
             23 JUMP_FORWARD             0 (to 26)
        >>   26 LOAD_CONST               0 (None)
             29 RETURN_VALUE
<module 'test' from 'test.py'>

The former is more Pythonic (better ideomatic code), but will not execute the block if A is False (not None).


if x: #x is treated True except for all empty data types [],{},(),'',0 False, and None

so it is not same as

if x is not None # which works only on None

A lot of functions return None if there are no appropriate results. For example, an SQLAlchemy query's .first() method returns None if there were no rows in the result. Suppose you were selecting a value that might return 0 and need to know whether it's actually 0 or whether the query had no results at all.

A common idiom is to give a function or method's optional argument the default value of None, and then to test that value being None to see if it was specified. For example:

def spam(eggs=None):
    if eggs is None:
        eggs = retrievefromconfigfile()

compare that to:

def spam(eggs=None):
    if not eggs:
        eggs = retrievefromconfigfile()

In the latter, what happens if you call spam(0) or spam([])? The function would (incorrectly) detect that you hadn't passed in a value for eggs and would compute a default value for you. That's probably not what you want.

Or imagine a method like "return the list of transactions for a given account". If the account does not exist, it might return None. This is different than returning an empty list (which would mean "this account exists but has not recorded transactions).

Finally, back to database stuff. There's a big difference between NULL and an empty string. An empty string typically says "there's a value here, and that value is nothing at all". NULL says "this value hasn't been entered."

In each of those cases, you'd want to use if A is None. You're checking for a specific value - None - not just "any value that happens to cast to False".


As written in PEP8:

  • Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators.

    Also, beware of writing "if x" when you really mean "if x is not None" -- e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!