Python list remove method: how's the implementation? -


in java, have client class have "code" attr, , equals method. method equals receives client , compares itself's code attr.

in python, read have __cmp__ method, same java method equals. ok, did that. created class client, "code" attr , method comp verify if code 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 put 3 client objects in python's list:

bla = [client(1), client(2), client(3)] 

then, when try:

bla.remove(client(3)) 

the amazing python removes first element (the client code 1).

what doing wrong? searched implementation of list in python's lib files, not easy find.

anyone can help?

http://docs.python.org/reference/datamodel.html#object.__cmp__

__cmp__(self, other)

called comparison operations if rich comparison (see above) not defined. should return negative integer if self < other, 0 if self == other, positive integer if self > other.

basically, should change implementation of __cmp__ be...

def __cmp__(self, obj):     return cmp(obj.code, self.code) 

the cmp() builtin function of python designed return values __cmp__ expected return comparing 2 arguments.

there different function in python called __eq__ checks equality, current implementation of __cmp__ better suited.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -