views:

222

answers:

4

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 dictionaries which contain native data types. In these cases the == operator checks if two objects are equal. But in some other cases (trying to compare instances of classes I created) the == operator just checks if the two variables reference the same object (so in these cases the == operator is equivalent to the is operator)

My question is: When does the == operator do more than just comparing identities?

EDIT: I'm using Python 3

+8  A: 

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:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

the last one should probably replace null with None, but the rules are not as easy here in Python as in Java.

Dirk
Thanks for the rule set for implementing '__eq__'
snakile
This "rules" have strong mathematical root: http://en.wikipedia.org/wiki/Equivalence_relation
Tomasz Wysocki
+11  A: 

== 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;-).

Alex Martelli
+1  A: 

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).

Tony Veijalainen
What makes 'if myvalue is None' better than 'if myvalue == None'?
snakile
See PEP8 for the rule. See Alex' comments about __eq__, additionally it is faster: http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html
Tony Veijalainen
@Tony: OK, thanks. +1
snakile
+2  A: 

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)
brone
interesting. +1
snakile