Python traps · Python
Python: 3 keys in, 1 key out?!PRO
1, True and 1.0 are equal and hash the same, so they are ONE dict key. The last value wins.
# 3 keys in, 1 key out ?!
# 1, True and 1.0 are EQUAL in Python and have the same hash, so they are the same dict key.
d = {}
d[1] = "a"
d[True] = "b"
d[1.0] = "c"
print(d) # {1: 'c'} ?!
print(1 == True == 1.0) # True
print(hash(1), hash(True), hash(1.0)) # 1 1 1
Open code P8 in Pocket Compiler
Download the Pocket Compiler app — free
No app yet? Install it, then open the app and type the code P8 in Code Library.