Last active
February 16, 2019 01:45
-
-
Save juanarrivillaga/e26a8d8188015324f170931abb309770 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You are looking for the built-in `dict` data-type. However, keys must be *hashable* objects. | |
Python prevents you from shooting yourself in the foot by using a mutable object as a key | |
(although, you can implement your own data-type which is mutable and hashable, but you *shouldn't*). | |
Note, consider the following JS code: | |
> m1 = new Map([['a', 1]]) | |
Map { 'a' => 1 } | |
> m2 = new Map() | |
Map {} | |
> m2.set(m1, 3) | |
Map { Map { 'a' => 1 } => 3 } | |
> m2.get(m1) | |
3 | |
Seems ok at first blush. Ideally, though, you want keys with the *same value* to retrieve the corresponding | |
value from the map. So... | |
> m2.get(new Map([['a',1]])) | |
undefined | |
So really, how useful is this map? | |
What JS does under the hood is hash by *identity* in this case. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment