Guest User

Untitled

a guest
Jul 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class A(object):
  2. def __init__(self, id, string):
  3. self.id = id
  4. self.string = string
  5.  
  6. def __eq__(self, other):
  7. if not hasattr(other, 'id'):
  8. return False
  9. return other.id == self.id
  10.  
  11. def __hash__(self):
  12. return hash(self.id)
  13.  
  14. def __cmp__(self, other):
  15. if not hasattr(other, 'id'):
  16. return cmp(self.id, other)
  17. return cmp(self.id, other.id)
  18.  
  19. a = A(1, "banana")
  20. b = A(1, "abcdef")
  21.  
  22. assert a == b
  23. assert hash(a) == hash(b)
  24.  
  25. assert not a < b
  26. assert not b < a
  27.  
  28. l = [a]
  29. assert b in l
  30.  
  31. d = {b: 1}
  32. assert d[a] == 1
Add Comment
Please, Sign In to add comment