Skip to content

Commit d4a60ac

Browse files
committed
Update example: Time for more Hash brownies!
Remove previously ambiguous explnanation and add more explanatory and clear explanation. Fixes satwikkansal#10
1 parent 8216f02 commit d4a60ac

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,16 @@ some_dict[5] = "Python"
244244

245245
#### 💡 Explanation
246246

247-
* `5` (an `int` type) is implicitly converted to `5.0` (a `float` type) before calculating the hash in 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 hash in Python.
248249
```py
250+
>>> 5 == 5.0
251+
True
249252
>>> hash(5) == hash(5.0)
250253
True
251254
```
255+
**Note:** Objects with different values may also have same hash (known as hash 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`.
252257
* This StackOverflow [answer](https://stackoverflow.com/a/32211042/4354153) explains beautifully the rationale behind it.
253258

254259
---

0 commit comments

Comments
 (0)