mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-12-22 21:00:09 +01:00
Update example: Time for more Hash brownies!
Remove previously ambiguous explnanation and add more explanatory and clear explanation. Fixes https://github.com/satwikkansal/wtfpython/issues/10
This commit is contained in:
parent
8216f02618
commit
d4a60ac9d0
7
README.md
vendored
7
README.md
vendored
@ -244,11 +244,16 @@ some_dict[5] = "Python"
|
||||
|
||||
#### 💡 Explanation
|
||||
|
||||
* `5` (an `int` type) is implicitly converted to `5.0` (a `float` type) before calculating the hash in Python.
|
||||
* Python dictionaries check for equality and compare the hash value to determine if two keys are the same.
|
||||
* Immutable objects with same value always have a same hash in Python.
|
||||
```py
|
||||
>>> 5 == 5.0
|
||||
True
|
||||
>>> hash(5) == hash(5.0)
|
||||
True
|
||||
```
|
||||
**Note:** Objects with different values may also have same hash (known as hash collision).
|
||||
* 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`.
|
||||
* This StackOverflow [answer](https://stackoverflow.com/a/32211042/4354153) explains beautifully the rationale behind it.
|
||||
|
||||
---
|
||||
|
Loading…
x
Reference in New Issue
Block a user