Various Python guides say to use x is None instead of x == None. Why is that? Equality is used for comparing values, so it seems natural to ask if x has the value None, denoted with == and not is. Can someone explain why is is the preferred form and show an example where the two do not give the same answer?
Thanks.
解决方案
The reason people use is is because there is no advantage to using ==. It is possible to write objects that compare equal to None, but it is uncommon.
class A(object):
def __eq__(self, other):
return True
print A() == None
Output:
True
The is operator is also faster, but I don't consider this fact important.