Search code examples
pythonoopcmp

Python Object Comparison without defining __cmp__ for the class


Suppose I create a class in python, but I don't define a __cmp__ method for my class. Now I create two instances of that class and compare them. What cmp method does Python use? I ran the code and it returns false when comparing for equality. Does Python compare memory addresses?

class A(object):
    def __init__(self, s):
        self.s = s

    def __str__(self):
        return self.s

x1 = A("jim")
x2 = A("jim")
print x1 == x2

Shell says:

False

Solution

  • lambda x,y: id(x)==id(y) if memory serves.