
Answer 1
The answer is explained here.
To quote:
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.
is None is a bit (~50%) faster than == None 🙂 – Nas Banov
Answer 2
is always returns True if it compares the same object instance
Whereas == is ultimately determined by the __eq__() method
i.e.
1 |
class : |
Answer 3
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:
1 |
p = [1] |
But since there is only one None, they will always be the same, and is will return True.
1 |
p = None |
Answer 4
(ob1 is ob2) equal to (id(ob1) == id(ob2))
… but (ob is ob2) is a LOT faster. Timeit says “(a is b)” is 0.0365 usec per loop and “(id(a)==id(b))” is 0.153 usec per loop. 4.2x faster! – AKX
{} is {} is false and id({}) == id({}) can be (and is in CPython) true.




近期评论