I have a class MyClass, which contains two member variables foo and bar:
class MyClass:
def __init__(self, foo, bar):
self.foo = foo
self.bar = bar
I have two instances of this class, each of which has identical values for foo and bar:
x = MyClass('foo', 'bar')
y = MyClass('foo', 'bar')
However, when I compare them for equality, Python returns False:
>>> x == y
False
How can I make python consider these two objects equal?
you have to tell python how exactly you want equality be defined.
do so by defining a special method
__eq__like this:the
__cmp__(self, other)is the “old” style to compare instances of classes, and only used when there is norich comparisonspecial method found. read up on them here: http://docs.python.org/release/2.7/reference/datamodel.html#specialnames