The “does Python have variables?” debate

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed May 7 21:27:08 EDT 2014


On Wed, 07 May 2014 16:35:00 -0500, Mark H Harris wrote:

> On 5/7/14 4:15 PM, Marko Rauhamaa wrote:
>>> That's why I always try to say “Python doesn't have variables the way
>>> you might know from many other languages”,
>>
>> Please elaborate. To me, Python variables are like variables in all
>> programming languages I know. Python currently does not allow me to
>> obtain the "address" of a variable, but that doesn't make me think of
>> Python variables any differently.
> 
> Here is the difference...  in almost every other language you know.

How do you know which languages Marko knows?

> A = macTruck
> B = A
> 
> In almost every other language you know A and B each "contain" by
> reference (and almost always by static type) macTruck. But NOT python.

Nor Javascript, Ruby, Perl, PHP, Lua, or (I think) Lisp or Java. To 
mention only a few.

I think it is easy to exaggerate the difference between Python and 
"almost every other language". Python's name binding model is very common 
amongst dynamically typed languages, and there are many dynamically typed 
languages.


If I have understood correctly, and I welcome confirmation or correction, 
one can have any combination of:

* dynamic typing and name binding (e.g. Python and Ruby);
* static typing and name binding (e.g. Java);
* dynamic typing and fixed-location variables (any examples?);
* static typing and fixed-location variables (C, Pascal).

Where do VB and Perl fit into this?


There are probably other variations as well. E.g. Cobra has both dynamic 
typed variables and static typed variables.


> In almost every other language you know there is some kind of binding
> table (name reference to type/length) and BOTH A and B are bound to data
> in memory macTruck... and usually its a different place in memory!
> 
> NOT python.

Python has "binding tables" too. They are dicts.

Any language with variables must have some way of mapping the name to the 
value. In some languages that mapping is available only to the compiler, 
and no longer exists at runtime (e.g. the assignment "x := 1;" is 
compiled to the equivalent of "copy value 1 into memory location 1234", 
with no reference to "x" remaining). In some languages the mapping is 
only available at runtime (e.g. the assignment "x := 1;" is compiled to 
the equivalent of "look up x and bind the value 1 to it"). In Python, 
lookups occur at runtime, not compile time.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/



More information about the Python-list mailing list