1. So I am returning 1 for every instance of MyClass and then inserting
the instances into a set. However when I print the set I see individual
instances though I want the instances to be the treated the same .ie there
should be just one instance in the set because the return value from
__hash__ is always 1.
In the dictionary when I print it I see the instances as individual keys
though I want to see a single key - what gives?? Why is the return value
of __hash__ being ignored?? (I can see it's being called)
2. When is __eq__ used? If I have two instances and want to make them
equal - what's the use case?
#!/usr/bin/python
class MyClass:
def __bool__(self):
print('in __bool__')
return True
def __hash__(self):
# hash value is not a key
print('in __hash__')
return 1
def __eq__(self, val):
print('in __eq__', val)
return True
c = MyClass()
d = MyClass()
if c:
print("true")
print('-----')
s = set()
s.add(c)
s.add(d)
print(s)
print('-----')
d = { c : "10", d : "20" }
print(d)
--
https://mail.python.org/mailman3//lists/python-list.python.org