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