In java, I have my client class that have the “code” attr, and the equals method. Method equals receives another client and compares with itself’s code attr.
In python, I just read that we have the __cmp__ method, to do the same as java method equals. Ok, I did that. I created my class client, with “code” attr and the method comp that verify if the code is the same.
class Client():
def __init__(self, code):
self.code = code
def __cmp__(self, obj):
return obj.code == self.code
def __repr__(self):
return str(self.code)
Then I put 3 Client objects in a python’s list:
bla = [Client(1), Client(2), Client(3)]
Then, when I try:
bla.remove(Client(3))
The amazing python removes the first element (the Client with code 1).
What I am doing wrong? I searched the implementation of list in python’s Lib files, but is not easy to find.
Anyone can help?
http://docs.python.org/reference/datamodel.html#object.__cmp__Basically, you should change your implementation of
__cmp__to be…The
cmp()builtin function of Python is specifically designed to return the values that__cmp__is expected to return by comparing its two arguments.There is also a different function in Python called
__eq__which only checks equality, for which your current implementation of__cmp__would be better suited.