From d4a60ac9d008d47b7263b532ec048d4ff35323aa Mon Sep 17 00:00:00 2001 From: Satwik Kansal Date: Mon, 4 Sep 2017 23:39:23 +0530 Subject: [PATCH] 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 --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2fa795e..2674f0d 100755 --- a/README.md +++ b/README.md @@ -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. ---