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
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 theequalsmethod, namely:the last one should probably replace
nullwithNone, but the rules are not as easy here in Python as in Java.