You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+6-1Lines changed: 6 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -244,11 +244,16 @@ some_dict[5] = "Python"
244
244
245
245
#### 💡 Explanation
246
246
247
-
*`5` (an `int`type) is implicitly converted to `5.0` (a `float`type) before calculating the hashin Python.
247
+
* Python dictionaries check for equality and compare the hash value to determine if two keys are the same.
248
+
* Immutable objects with same value always have a same hashin Python.
248
249
```py
250
+
>>>5==5.0
251
+
True
249
252
>>>hash(5) ==hash(5.0)
250
253
True
251
254
```
255
+
**Note:** Objects with different values may also have same hash (known ashash collision).
256
+
* When the statement `some_dict[5] = "Python"`is executed, the existing value "JavaScript"is overwritten with"Python" because Python recongnizes `5`and`5.0`as the same keys of the dictionary `some_dict`.
252
257
* This StackOverflow [answer](https://stackoverflow.com/a/32211042/4354153) explains beautifully the rationale behind it.
0 commit comments