id( ) function question

Stephen Hansen apt.shansen at gmail.com
Wed Oct 14 19:32:50 EDT 2009


On Wed, Oct 14, 2009 at 4:19 PM, Chris Rebert <clp2 at rebertia.com> 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)
6296752
>>> 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
6296752
>>> id({}) == id({})
True
>>> 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)
6296752
>>> id(y)
6299344
>>> id({})
6349136
>>> id({})
6349136
>>> z = {}
>>> id(z)
6349136
>>> id({})
7214832

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091014/5037390f/attachment-0001.html>


More information about the Python-list mailing list