id( ) function question

Mel mwilson at the-wire.com
Wed Oct 14 22:13:35 EDT 2009


Chris Rebert wrote:
> Although I have no idea how it is that `id({}) == id({})` as a prior
> posted showed; FWIW, I can't manage to reproduce that outcome.

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> id({})== id({})
True


It's believable if id({}) does the following:

1. Construct an empty dict
2. Take the id of the dict
3. Reduce the reference-count on the now-unneeded dict.

It's not too hard for the second empty dict to get allocated in the same 
memory that the first one (now dereferenced and deallocated) used, so 
CPython gives it the same id value.

When the == comparison happens, all it needs are the two ints returned from 
the id calls.

	Mel.





More information about the Python-list mailing list