Dictionaries with tuples or tuples of tuples
msirenef at lightbird.net
Tue Feb 19 03:54:19 CET 2013
On 02/18/2013 09:17 PM, Jon Reyes wrote:
> Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though:
> " so that if two
> key objects are equal, they stay equal, and if they differ, they stay
> different. "
> What does this mean? I won't be comparing key objects with one
another. Also, when I had two keys with the same value the value of the
other key disappeared so I assume in runtime if there are multiple keys
of the same value only the last one will appear.
You won't be, but dict will.
Dict is by definition a mapping where a value is assigned to a unique
key. If you have two keys and two values, and then change one key to
be equal to the second key, that's not kosher, because which value it's
supposed to return when you try to get it by that key?
So in effect, key's hash value should not change. If key is immutable,
you can be certain that it's hash value will not change. If it's
mutable, you have to make sure not to change the key in a way that'd
make its hash value different than it was.
Lark's Tongue Guide to Python: http://lightbird.net/larks/
Graphic design is the paradise of individuality, eccentricity, heresy,
abnormality, hobbies and humors. George Santayana
More information about the Python-list