Explaining names vs variables in Python

Steven D'Aprano steve at pearwood.info
Wed Mar 2 05:32:36 EST 2016


On Wed, 2 Mar 2016 08:03 pm, Jesper K Brogaard wrote:

> As I understand it, when you use 'is', you are comparing addresses to
> objects, not the values contained in the objects. Use '==' instead.

You should not think about addresses, because the location of objects is not
part of the language. It is implementation-dependent.

In CPython, objects are fixed to a single location in the heap, and will
never move. But in Jython and IronPython, objects in the python layer are
based on JVM (Java Virtual Machine) and CLR (Common Language Runtime)
objects, which can move around in memory. Both the JVM and the CLR can move
objects, so the location is not constant. Likewise for PyPy, which can
delete and re-create objects behind the scenes, possibly in different
memory locations.

This is why the id() function is NOT documented as returning the address of
an object, but of returning an ID number. Let's look at IDs in IronPython:

>>> a, b, c = [], 10000, "Hello world!"
>>> print id(a), id(b), id(c), id(None)
43 44 45 0


And in Jython:

>>> a, b, c = [], 10000, "Hello world!"
>>> print id(a), id(b), id(c), id(None)
1 2 3 4




-- 
Steven




More information about the Python-list mailing list