Can I use:
if A:
instead of
if A is not None:
The latter seems so verbose. Is there a difference?
The statement
if A:
will call A.__bool__()
(see Special method names documentation), which was called __nonzero__
in Python 2, and use the return value of that function. Here's the summary:
object.__bool__(self)
Called to implement truth value testing and the built-in operation
bool()
; should returnFalse
orTrue
. When this method is not defined,__len__()
is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither__len__()
nor__bool__()
, all its instances are considered true.
On the other hand,
if A is not None:
compares only the reference A
with None
to see whether it is the same or not.
A is not None
is faster since there is much less work to do
Commented
Oct 19, 2011 at 4:31
if object(): pass
is ~0.130 usec per loop, while if object() is not None: pass
is ~0.135 usec. Anyway, you should not use performance to choose between these two, but rather look at the differences in how they work, since they are not equivalent.
Commented
Oct 19, 2011 at 8:02
if A is not None
seems to be slower because it's a comparison and needs to load the builtin singleton None
as an intermediary step to compare with A
(take a look at the dis.dis()
). Correct me if I'm wrong but if A:
seems to be more efficient, as soon as you really wants to test the truth value and not the None
identity.
python -m timeit -s"a=0" "if a: pass" "else: pass"
is faster than python -m timeit -s"a=0" "if a is None: pass" "else: pass"
but python -m timeit -s"a=1" "if a: pass" "else: pass"
is slower. Might be platform dependant, see if you get the same results
Commented
Mar 25, 2013 at 6:07
is None
test was indeed the slowest for me. In pypy they all measured exactly the same :)
Commented
Mar 25, 2013 at 6:09
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!
None
, but just in a truth value checking. In this case, if A:
seems more efficient (take a dis.dis()
, there is the extra steps of loading the builtin None
and comparing, with if A is not None:
, while there is only a jump_if
in the other case).
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
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.
None
is a singleton, it's not an implementation detail (unlike int or string interning). I'm not sure I follow what you mean.
Commented
Feb 7, 2017 at 9:02
None
behaves differently than int
or str
due to interning. My point is that is
and ==
are checking different things; first checks a memory address, the second checks the contents of memory addresses.
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".
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.
if A:
will prove false if A is 0, False, empty string, empty list or None, which can lead to undesired results.
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.
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.
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.
The former is more Pythonic (better ideomatic code), but will not execute the block if A is False (not None).
is/is not None
. Following PEP8 is Pythonic. Besides the two tests are different.
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'>
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: