builtin_hash.py 892 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # test builtin hash function
  2. print(hash(False))
  3. print(hash(True))
  4. print({():1}) # hash tuple
  5. print({(1,):1}) # hash non-empty tuple
  6. print(hash in {hash:1}) # hash function
  7. try:
  8. hash([])
  9. except TypeError:
  10. print("TypeError")
  11. class A:
  12. def __hash__(self):
  13. return 123
  14. def __repr__(self):
  15. return "a instance"
  16. print(hash(A()))
  17. print({A():1})
  18. # all user-classes have default __hash__
  19. class B:
  20. pass
  21. hash(B())
  22. # if __eq__ is defined then default __hash__ is not used
  23. class C:
  24. def __eq__(self, another):
  25. return True
  26. try:
  27. hash(C())
  28. except TypeError:
  29. print("TypeError")
  30. # __hash__ must return an int
  31. class D:
  32. def __hash__(self):
  33. return None
  34. try:
  35. hash(D())
  36. except TypeError:
  37. print("TypeError")
  38. # __hash__ returning a bool should be converted to an int
  39. class E:
  40. def __hash__(self):
  41. return True
  42. print(hash(E()))