添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams It's unclear if this question is about is vs == , or about the nature of what exactly None is and how the behaviour differs in either context (the latter is why I ended up here). Based on the vagueness and lack of OP responses... I'm surprised this has so many upvotes. I mean... cmon... the question is not even written in the actual question... RTbecard Jan 22, 2020 at 18:42 Does this answer your question? Is there any difference between "foo is None" and "foo == None"? user3064538 Sep 15 at 7:45

A class is free to implement comparison any way it chooses, and it can choose to make comparison against None mean something (which actually makes sense; if someone told you to implement the None object from scratch, how else would you get it to compare True against itself?).

Practically-speaking, there is not much difference since custom comparison operators are rare. But you should use is None as a general rule.

@myusuf3: >>> timeit.Timer('None is None').timeit() | 0.225 | >>> timeit.Timer('None == None').timeit() | 0.328 Nas Banov Jan 25, 2012 at 21:44 @myusuf3 You don't really need a proof for that. is is, basically, integer comparison while == is not only resolving references but comparing values which may have mismatching types. Pijusn Aug 9, 2013 at 15:40 One on favour of "is". When a variable can be either None or something that has no meaningful comparison with None. For example, a variable can be a numpy.array or None (my particular case). Jblasco Oct 14, 2014 at 10:38 I would like to add to what @TimLudwinski is saying: first, if someone chose to override the equality operator to make None a special case, why would we want to tell them otherwise? Second, "There should be one-- and preferably only one --obvious way to do it." And the obvious way to check if something is equal to something is, well, the equality operator. Yuval Dec 14, 2015 at 16:45

In this case, they are the same. None is a singleton object (there only ever exists one None ).

is checks to see if the object is the same object, while == just checks if they are equivalent.

For example:

p = [1]
q = [1]
p is q  # False because they are not the same actual object
p == q  # True because they are equivalent

But since there is only one None, they will always be the same, and is will return True.

p = None
q = None
p is q  # True because they are both pointing to the same "None"
                This answer is not correct, as explained in Ben Hoffstein's answer below stackoverflow.com/questions/3257919/is-none-vs-none/…. x == None may evaluate to True even if x is not None but an instance of some class with its own custom equality operator.
– max
                Nov 16, 2010 at 3:00

It depends on what you are comparing to None. Some classes have custom comparison methods that treat == None differently from is None.

In particular the output of a == None does not even have to be boolean !! - a frequent cause of bugs.

For a specific example take a numpy array where the == comparison is implemented elementwise:

import numpy as np
a = np.zeros(3) # now a is array([0., 0., 0.])
a == None #compares elementwise, outputs array([False, False, False]), i.e. not boolean!!!
a is None #compares object to object, outputs False
                I think this is a better answer than the current accepted answer, as it has easy to understand examples.
– Olsgaard
                Aug 6, 2020 at 7:56