Why Python Overwrites Dict Entries with 5 vs 5.0 and Other Hidden Behaviors
This article explores three surprising Python quirks: how numeric keys like 5 and 5.0 collide in dictionaries, why a return statement in a finally block overrides earlier returns, and how object identity and hash values can behave unexpectedly, revealing the language’s underlying mechanics.
First: Mysterious Dictionary Keys
Python dictionaries consider keys equal if they compare equal and have the same hash value. Immutable objects with the same value always have the same hash.
some_dict = {}
some_dict[5.5] = "Ruby"
some_dict[5.0] = "JavaScript"
some_dict[5] = "Python"Output:
>> some_dict[5.5]
"Ruby"
>>> some_dict[5.0]
"Python"
>>> some_dict[5]
"Python"Python dictionaries determine key equality by checking both equality and hash value.
Immutable objects with identical values share the same hash.
Note: Different objects can also share the same hash (hash collisions).
>> 5 == 5.0
True
>>> hash(5) == hash(5.0)
TrueWhen executing some_dict[5] = "Python", the key 5 is recognized as the same key as 5.0, so the existing value "JavaScript" is overwritten.
Second: Return in Exception Handling
def some_func():
try:
return 'from_try'
finally:
return 'from_finally'Output:
>> some_func()
'from_finally'In a try...finally block, the finally clause always runs, even after return, break, or continue.
The function’s return value is determined by the last executed return statement; because finally always executes, its return overrides earlier returns.
Third: Equality of Identical Objects
class WTF:
passOutput:
>> WTF() == WTF() # two different objects should not be equal
False
>>> WTF() is WTF()
False
>>> hash(WTF()) == hash(WTF())
True
>>> id(WTF()) == id(WTF())
TrueThe id() function creates a temporary object, passes it to id, then discards it; the next call may reuse the same memory address.
Thus, id values are only unique during an object’s lifetime; after destruction, other objects can receive the same id.
The is operator checks object identity, which remains False because the objects are distinct, even if their id values coincide.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
