Reputation: 22592
Is there any difference between:
if foo is None: pass
and
if foo == None: pass
The convention that I've seen in most Python code (and the code I myself write) is the former, but I recently came across code which uses the latter. None is an instance (and the only instance, IIRC) of NoneType, so it shouldn't matter, right? Are there any circumstances in which it might?
Upvotes: 234
Views: 36458
Reputation: 3211
(ob1 is ob2)
is functionally identical to (id(ob1) == id(ob2))
Upvotes: 13
Reputation: 719
You may want to read this: object identity and equivalence.
The is
operator is used for testing object identity; it checks if two objects refer to the same address in memory.
The ==
operator is use for testing for equivalence; it checks if two objects have equivalent state.
Upvotes: 51
Reputation: 26108
A word of caution:
if foo:
# do something
Is not exactly the same as:
if foo is not None:
# do something
The former is a boolean value test and can evaluate to false in different contexts. There are a number of things that represent false in a boolean value tests for example empty containers, boolean values. None
also evaluates to false in this situation but other things do too.
Upvotes: 28
Reputation: 19353
is
always returns True
if it compares the same object instance, whereas ==
is ultimately determined by the __eq__()
method.
i.e.
>>> class Foo:
def __eq__(self, other):
return True
>>> f = Foo()
>>> f == None
True
>>> f is None
False
Upvotes: 277
Reputation: 525
a is b # returns true if they a and b are true alias
a == b # returns true if they are true alias or they have values that are deemed equivalence
a = [1,3,4]
b = a[:] #creating copy of list
a is b # if gives false
False
a == b # gives true
True
Upvotes: 0
Reputation: 29514
is
tests for identity, not equality. For your statement foo is none
, Python simply compares the memory address of objects. It means you are asking the question "Do I have two names for the same object?"
==
on the other hand tests for equality as determined by the __eq__()
method. It doesn't cares about identity.
In [102]: x, y, z = 2, 2, 2.0
In [103]: id(x), id(y), id(z)
Out[103]: (38641984, 38641984, 48420880)
In [104]: x is y
Out[104]: True
In [105]: x == y
Out[105]: True
In [106]: x is z
Out[106]: False
In [107]: x == z
Out[107]: True
None
is a singleton operator. So None is None
is always true.
In [101]: None is None
Out[101]: True
Upvotes: 9
Reputation: 6616
There is no difference because objects which are identical will of course be equal. However, PEP 8 clearly states you should use is
:
Comparisons to singletons like None should always be done with is or is not, never the equality operators.
Upvotes: 11
Reputation: 7129
Some more details:
The is
clause actually checks if the two object
s are at the same
memory location or not. i.e whether they both point to the same
memory location and have the same id
.
As a consequence of 1, is
ensures whether, or not, the two lexically represented object
s have identical attributes (attributes-of-attributes...) or not
Instantiation of primitive types like bool
, int
, string
(with some exception), NoneType
having a same value will always be in the same memory location.
E.g.
>>> int(1) is int(1)
True
>>> str("abcd") is str("abcd")
True
>>> bool(1) is bool(2)
True
>>> bool(0) is bool(0)
True
>>> bool(0)
False
>>> bool(1)
True
And since NoneType
can only have one instance of itself in the python's "look-up" table therefore the former and the latter are more of a programming style of the developer who wrote the code(maybe for consistency) rather then having any subtle logical reason to choose one over the other.
Upvotes: 3
Reputation: 4891
John Machin's conclusion that None
is a singleton is a conclusion bolstered by this code.
>>> x = None
>>> y = None
>>> x == y
True
>>> x is y
True
>>>
Since None
is a singleton, x == None
and x is None
would have the same result. However, in my aesthetical opinion, x == None
is best.
Upvotes: 1
Reputation: 27285
The reason foo is None
is the preferred way is that you might be handling an object that defines its own __eq__
, and that defines the object to be equal to None. So, always use foo is None
if you need to see if it is infact None
.
Upvotes: 13
Reputation: 57238
@Jason:
I recommend using something more along the lines of
if foo: #foo isn't None else: #foo is None
I don't like using "if foo:" unless foo truly represents a boolean value (i.e. 0 or 1). If foo is a string or an object or something else, "if foo:" may work, but it looks like a lazy shortcut to me. If you're checking to see if x is None, say "if x is None:".
Upvotes: 4
Reputation: 1553
For None there shouldn't be a difference between equality (==) and identity (is). The NoneType probably returns identity for equality. Since None is the only instance you can make of NoneType (I think this is true), the two operations are the same. In the case of other types this is not always the case. For example:
list1 = [1, 2, 3]
list2 = [1, 2, 3]
if list1==list2: print "Equal"
if list1 is list2: print "Same"
This would print "Equal" since lists have a comparison operation that is not the default returning of identity.
Upvotes: 5