id( ) function question

Chris Rebert clp2 at rebertia.com
Thu Oct 15 01:19:52 CEST 2009


On Wed, Oct 14, 2009 at 1:37 PM, Laszlo Nagy <gandalf at shopzeus.com> wrote:
>> Andre Engels schrieb:
>>> What is going on is that a few objects that are often used, in
>>> particular the small (how small is small depends on the
>>> implementation) integers, are 'preloaded'. When one of these is then
>>> referred to, a new object is not created, but the pre-defined object
>>> is used. 10 is apparently a preloaded constant in your implementation,
>>> 1e10 is not.
>>>
>>> As far as I know, only None is _guaranteed_ to be such a preloaded
>>> object, so one should not rely on it in implementations.
>>>
>>
>> None, True, False, NotImplemented are guaranteed to be singletons, all
>> builtin types and exceptions can be considered as singletons, too.
>>
>
> I thought that different mutable objects always have different ids. If this
> is not true, then what the id() function is used for? What useful thing can
> we do with it?

The built-ins aren't mutable, and the singletons are each immutable
and/or unique; so in no case do objects that are both different and
mutable have the same ID.
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.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list