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

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.