osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Explanation of list reference


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: 
http://nedbatchelder.com/text/names.html

-- 
Ned Batchelder, http://nedbatchelder.com