Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from https://gist.github.com/astynax/0f759f1527d6bdcf3d26c48837c59eb8
- >>> class Point:
- ... def __init__(self, x, y):
- ... self.x, self.y = x, y
- ... def __hash__(self):
- ... return hash((self.x, self.y))
- ... def __eq__(self, other):
- ... return (self.x, self.y) == (other.x, other.y)
- ... def __repr__(self):
- ... return "Point({},{})".format(self.x, self.y)
- ...
- >>> p = Point(10, 20)
- >>> d = {p: 'a'}
- >>> d
- {Point(10,20): 'a'}
- >>> p.x += 1
- >>> d[p] = 'b'
- >>> d
- {Point(11,20): 'b', Point(11,20): 'a'}
- >>> ks = list(d.keys())
- >>> ks[0] == ks[1]
- True
- >>> hash(ks[0]) == hash(ks[1])
- True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement