custom classes in sets
vegetax
vegeta.z at gmail.com
Mon Feb 14 03:13:49 EST 2005
Steven Bethard wrote:
> 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:
I just tried and it wont be called =(, so how can i generate a hash code for
the CFile class? note that the comparitions(__eq__) are done based on the
contents of a file using the command 'cmp', i guess thats not posible but
thanks.
More information about the Python-list
mailing list