Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A(object):
- def __init__(self, id, string):
- self.id = id
- self.string = string
- def __eq__(self, other):
- if not hasattr(other, 'id'):
- return False
- return other.id == self.id
- def __hash__(self):
- return hash(self.id)
- def __cmp__(self, other):
- if not hasattr(other, 'id'):
- return cmp(self.id, other)
- return cmp(self.id, other.id)
- a = A(1, "banana")
- b = A(1, "abcdef")
- assert a == b
- assert hash(a) == hash(b)
- assert not a < b
- assert not b < a
- l = [a]
- assert b in l
- d = {b: 1}
- assert d[a] == 1
Add Comment
Please, Sign In to add comment