id( ) function question

Stephen Hansen apt.shansen at
Thu Oct 15 01:32:50 CEST 2009

On Wed, Oct 14, 2009 at 4:19 PM, Chris Rebert <clp2 at> 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.

With Python 2.5.1 on MacOS X, I can; it looks like there's an optimization
in there where its 'saving' dictionaries that are created but not used, and
then using those saved dictionaries until they're really used, instead of
making new ones all the time.

At least that's how I interpret this output (comments added after-the-fact):
>>> id({}) # at this point a dictionary appears to be created  (though it
may have happened earlier)
>>> id({}) # previous dictionary wasn't used, so was saved; and now instead
of creating a new one, it'll just use what's on the waiting list
>>> id({}) == id({})
>>> x = {} # that same dictionary still hasn't been used, but now it is
right here-- so the waiting list is empty.
>>> y = {} # and a new one is made!
>>> id(x)
>>> id(y)
>>> id({})
>>> id({})
>>> z = {}
>>> id(z)
>>> id({})

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list