Christian Heimes írta:
Chris Rebert wrote:
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.
Correct, the fact allows you to write code like "type(egg) is str" to
check if an object *is* an instance of str. The isistance() methods also
returns True if the argument is a subclass of str.
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.
The behavior is caused by a free list in the dict implementation. When
the reference count of a object drops to zero, it's usually removed from
memory entirely. However some types keep a list of unused objects around
to increase efficiency and spare some calls to malloc() and free().
For the code "{} is {}" Python has to create two distinct dict objects.
"id({}) == id({})" returns True under most circumstances because the
reference count of the first dict drops to zero as soon as the id()
function returns its memory address. The second call to id() retrieves
the very same template from the dict type's free list thus returning the
same memory address.
All right, I see your point now. So can we say, that the id function can
be used to tell if two mutable objects are different as long as they are
both alive during the comparison?
--
http://mail.python.org/mailman/listinfo/python-list