vegetax wrote:
How can i make my custom class an element of a set?

class Cfile:
  def __init__(s,path): s.path = path

  def __eq__(s,other):
   print 'inside equals'
   return not os.popen('cmp %s %s' % (s.path,other.path)).read()

  def __hashcode__(s): return s.path.__hashcode__()

the idea is that it accepts file paths and construct a set of unique files (the command "cmp" compares files byte by byte.),the files can
have different paths but the same content


but the method __eq__ is never called

Seems to be called fine for me:

py> class Cfile:
... def __eq__(self, other):
... print 'inside equals'
... return False
... def __hash__(self):
... return 0
...
py> {Cfile():1, Cfile():2}
inside equals
{<__main__.Cfile instance at 0x01166490>: 1, <__main__.Cfile instance at 0x01166760>: 2}


Note that __eq__ won't be called if the hashes are different:

py> class Cfile:
... hash = 0
... def __eq__(self, other):
... print 'inside equals'
... return False
... def __hash__(self):
... Cfile.hash += 1
... return Cfile.hash
...
py> {Cfile():1, Cfile():2}
{<__main__.Cfile instance at 0x01166918>: 1, <__main__.Cfile instance at 0x011668A0>: 2}


Steve
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to