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

Your problem is that your class defines an insane hash. For a sane hash, A == B implies hash(A) == hash(B), whereas you have based your hash on the path to the files, but the comparison on the contents of the files. If the paths are all different, then nothing is likely to hash the same, so the set will never get around to trying the direct comparison.


Change the hash method to use the size of the file or something else that has to be equal for the comparison to be equal (like the hash of the first line, or of the entire file), and you should see much better behaviour.

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
            http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to