I'm using Python 3.6. Suppose that I'm trying to keep a unique set of tuples. I know I can use tuple in set and get back the correct result (the set contains the tuple or not).
s = set() t1 = (1, 2) t2 = (1, 2) s.add(t1) print(t2 in s) # True -- Great! Now, suppose I have a custom class that contains a tuple. I would like to define uniqueness for the custom class objects as uniqueness for the tuple. I did the following:
class TupleWrapper(object): def __init__(self, t): self.t = t # tuple def __hash__(self): return hash(self.t) s = set() t1 = TupleWrapper((1, 2)) s.add(t1) t2 = TupleWrapper((1, 2)) print(t2 in s) # False -- WHY? I wrote my own __hash__() method that hashes the tuple. So why are the two TupleWrapper objects with the same tuple not found to be the same in the set? Do I need to override another method?
没有评论:
发表评论