I noticed I can use the ==
operator to compare all the native data types (integers, strings, booleans, floating point numbers etc) and also lists, tuples, sets
==
and is
are always conceptually distinct: the former delegates to the left-hand object's __eq__
[1], the latter always checks identity, without any delegation. What seems to be confusing you is that object.__eq__
(which gets inherited by default by user-coded classes that don't override it, of course!) is implemented in terms of identity (after all, a bare object
has absolutely nothing to check except its identity, so what else could it possibly do?!-).
[1] omitting for simplicity the legacy concept of the __cmp__
method, which is just a marginal complication and changes nothing important in the paragraph's gist;-).
The ==
does more than comparing identity when ints are involved. It doesn't just check that the two ints are the same object; it actually ensures their values match. Consider:
>>> x=10000
>>> y=10000
>>> x==y,x is y
(True, False)
>>> del x
>>> del y
>>> x=10000
>>> y=x
>>> x==y,x is y
(True, True)
The "standard" Python implementation does some stuff behind the scenes for small ints, so when testing with small values you may get something different. Compare this to the equivalent 10000
case:
>>> del y
>>> del x
>>> x=1
>>> y=1
>>> x==y,x is y
(True, True)
In Python, the ==
operator is implemented in terms of the magic method __eq__, which by default implements it by identity comparison. You can, however, override the method in order to provide your own concept of object equality. Note, that if you do so, you will usually also override at least __ne__
(which implements the !=
operator) and __hash__
, which computes a hash code for the instance.
I found it very helpful, even in Python, to make my __eq__
implementations comply with the rules set out in the Java language for implementations of the equals method, namely:
the last one should probably replace null
with None
, but the rules are not as easy here in Python as in Java.
What is maybe most important point is that recommendation is to always use:
if myvalue is None:
not
if myvalue == None:
And never to use:
if myvalue is True:
but use:
if myvalue:
This later point is not so supper clear to me as I think there is times to separate the boolean True from other True values like "Alex Martelli" , say there is not False in "Alex Martelli" (absolutely not, it even raises exception :) ) but there is '' in "Alex Martelli" (as is in any other string).