I'm wrong or Will we fix the ducks limp?

BartC bc at freeuk.com
Mon Jun 6 08:35:57 EDT 2016


On 05/06/2016 07:37, Steven D'Aprano wrote:
> On Sun, 5 Jun 2016 01:17 pm, Lawrence D’Oliveiro wrote:
>
>> On Saturday, June 4, 2016 at 3:55:12 AM UTC+12, Matt Wheeler wrote:
>>> It's best to think of them as names, rather than variables, as names in
>>> python don't behave quite how you'll expect variables to if you're coming
>>> from some other languages.
>>
>> I have made heavy use of Python as well as many other languages, and I
>> don’t know what you mean. What “other languages” are you thinking of?
>>
>> Are variables like little boxes? Yes they are.
>
> That's *exactly* what variables in Python are not like.

People can imagine a box around a variable name if they like.

The confusion might arise when they think the value attached to the name 
is in the same box. But often, for variables holding or referring to 
simple scalar values, it doesn't matter.

>
>> But they are all the same
>> size, while objects may be large (even very large) or small. That is why
>> variables hold, not objects, but references to objects.
>
> No they don't. You are confusing the implementation with the programming
> model.
>
> Following the assignment:
>
> x = 99
>
> if you print(x), do you see something like "reference 0x12345"? No.

Nearly every high level language will perform an automatic dereference 
in such cases, between the written name of the variable (that sometimes 
corresponds to its /address/), and its value (for example, the contents 
of that address).

In Python there might well be a double dereference, from name to 
location, which contains an object reference, and from the object 
reference to the value that is printed.

While Python can give you access to the object reference rather than the 
object value, AFAIK you can't get the location where that object 
reference is stored for that variable. All you have is the name.

So for Python it makes more sense to have two boxes, one for the name, 
and another for the object value. With an arrow between the two. (Among 
other things, this allows same object to be shared between several 
variables: multiple boxed variable names all pointing to the same object 
box.)

But again, in the case of simple, immutable types, and where the 
variable always keeps the same type as is usually the case, there is no 
real problem in showing "x" and "99" in the same box, or "99" in the box 
and "x" immediately outside:

  x:[99]

instead of:

  x:[0x12345] -> 0x12345:[99]

-- 
Bartc


More information about the Python-list mailing list