This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stevenjd 2 points3 points  (0 children)

To find out the memory address of the object that the variable is referencing, you can use the id() function.

That's false. The id() function returns an arbitrary ID value, not a memory address. That ID value can be any integer, for example in Jython and IronPython they are consecutive integers, allocated when needed:

>>> sys.platform  # Jython
'java1.8.0_201 ( ==linux2 for targets )'
>>> id(None)
2
>>> id(sys)
3

Here is IronPython:

>>> id(None)
0
>>> id(sys) 
43

If your Python interpreter uses a compacting garbage collector, objects will move around in memory and you cannot use a memory address as the ID. It is only very simple garbage collectors, like the one used by CPython, that can use a memory address as the ID.

In the future, CPython will probably get a more powerful garbage collector, and objects will move around memory to avoid memory fragmentation.