Explanation of list reference
ned at nedbatchelder.com
Fri Feb 14 21:45:23 CET 2014
On 2/14/14 3:17 PM, Ian Kelly wrote:
> On Fri, Feb 14, 2014 at 12:56 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> There are two fundamentally different kinds of values in Python: "small"
>> values and "big" values. A variable can only hold a small value. A list
>> element can only hold a small value. A dictionary entry can only hold a
>> small value. The same is true for an object member (aka field).
>> So we have four kinds of (memory) slots: variables, list elements,
>> dictionary entries and fields. Any slot can only hold a small value.
>> The small values include numbers, booleans (True or False) and
>> references. All other values are big, too big to fit in a slot. They
>> have to be stored in a "vault" big enough to hold them. This vault is
>> called the heap. Big values cannot be stored in slots directly; instead,
>> references to big values are used.
> This is nonsense. Python the language makes no such distinction
> between "big" and "small" values. *All* objects in CPython are stored
> internally on the heap. Other implementations may use different
> memory management schemes.
Marko, I have to agree with Ian. While I really like the picture you
drew, the distinction between big and small values is pure fiction.
CPython does not store ints directly in list elements, for example.
All names are references to values. All list elements (and dictionary
values, dictionary keys, set elements, etc) are references to values. A
value can be another container like a list, or it can be something
"simple" like an int.
This covers all the details, including pictures like Marko's, but with
an explanation why we draw the ints inside the boxes:
Ned Batchelder, http://nedbatchelder.com
More information about the Python-list