Explanation of list reference
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Feb 15 02:03:17 EST 2014
On Fri, 14 Feb 2014 22:20:35 -0700, Ian Kelly wrote:
> On Fri, Feb 14, 2014 at 9:24 PM, Rustom Mody <rustompmody at gmail.com>
> wrote:
>> In the case of physical objects like dice there is a fairly
>> unquestionable framing that makes identity straightforward --
>> 4-dimensional space-time coordiantes. If the space-time coordinates of
>> 2 objects are all equal then the objects are identical, else not.
>>
>> Now we analogize the space-time identity of physical objects to
>> computer identity of computer objects (so-called) and all sorts of
>> problems ensue.
>>
>> To start with we say two objects are identical if they have the same
>> memory address.
>
> This is false. It happens to hold for CPython, but that's an
> implementation detail. The definition of object identity does not
> depend on memory address. It also doesn't have anything to do with
> space-time coordinates. The concept of object identity is an
> abstraction, not an analogy from physics.
Correct.
CPython does not move objects around in memory during their lifespan, so
CPython can reuse the memory address as the ID. Jython and IronPython do
move objects around, so they cannot use memory addresses as IDs. Instead
they number each object sequentially.
PyPy is even more complicated. Objects, like particles in quantum
mechanics, can appear and disappear from existence between observations
as the optimizing compiler does its work. For example, a list of Python
float objects may be transparently converted into an array of machine
doubles, then turned back into a Python object when you try to access it
again. The PyPy compiler has to take great care to ensure that the list
(and the floats!) get the same IDs before and after, since that is
defined behaviour in the language spec.
--
Steven
More information about the Python-list
mailing list