object identity and hashing

Jeff Schwab jeff at schwabcenter.com
Sun Feb 24 20:58:05 EST 2008


castironpi at gmail.com wrote:
> Can someone explain this?
> 
>>>> a= {}

Create an empty dict and bind it to the name a.

>>>> a[(3,)]= 0

Set the key/value pair (3,):0 to the dict.

>>>> (3,) in a

Is (3,) one of the keys in the dict?

> True

Yes, it is.

>>>> (3,) is (3,)

Create two separate tuples (that happen to be equivalent).  Are they the 
same object?

> False

No, they are not.

Every time you write (3,), you are potentially creating a new object. 
These objects have equal values (and hash codes), so they are 
interchangeable for purposes of keying a dict.




More information about the Python-list mailing list