The “does Python have variables?” debate

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 8 09:18:10 EDT 2014


On Thu, 08 May 2014 12:43:29 +0300, Marko Rauhamaa wrote:

> Ben Finney <ben at benfinney.id.au>:
> 
>> The concern is that the term “variable”'s existing baggage in the
>> programming community encourages *false inferences* that a beginner
>> doesn't even realise they're drawing. By discouraging use of that term
>> and replacing it with “name binding” or “reference”, a more accurate
>> model is implied, to the extent that its existing baggage encourages
>> significantly fewer false inferences.
> 
> I don't think flogging the beginner for talking about variables 

Nobody has suggested flogging anyone, not even figuratively. Unless you 
believe that correcting a misapprehension, no matter how gently it is 
done, is a flogging, I don't see how you draw the conclusion that Ben is 
talking about flogging anyone.


> helps
> them get Python's data model. All that accomplishes is that they will
> shut up about variables in the fear of being flogged and not understand
> the data model any better.

So far, we have had at least two people (myself and Jerry Hill) have 
stated that the names/values model has helped them learn the language. 
One of them (me) has even stated that the meme "Python has no variables" 
ACTIVELY HELPED (sorry for shouting) in learning the language as a 
beginner.

In contrast, we have some experts (like Ned) disagree with the meme, but 
I haven't seen anyone state that the meme "Python has no variables" has 
*hurt* their understanding. (I suspect that now I've raised this issue, 
some people will make a point of explicitly saying so, to counter my 
point.)

In any case, I regularly see people confused by wrongly applying C-like 
variable semantics to Python. E.g. the idea that Python may have pass by 
reference semantics comes up regularly. So is the idea that one can use 
id() to get a pointer to a variable, usually followed by the question 
"how do I dereference that pointer". 

I'm yet to see anyone whose understanding of Python was hurt by the "no 
variables" meme.

So... although I don't go quite so far as Ben in thinking that using the 
term "variable" in describing Python is harmful, I will say that the "no 
variables" meme is *useful* precisely because it is so shocking and so 
counter-intuitive that it forces[1] you to question your assumptions 
about how programming languages operate.


> Any C programmer will get Python easily because they are familiar with
> malloc() and pointers. 

They'll easily get Python *wrong* if they apply C semantics to Python, 
which many of them do.


> You will have more trouble with the beginner who
> has no prior programming knowledge. Do you first have to drag them along
> the keel by teaching them C and them graduating them to higher-level
> programming languages?

Good gods no. Teaching C is perhaps not as bad as teaching then BASIC, 
but that's a cruel and counter-productive thing to do to them if you want 
them to understand Python semantics. If you want them to learn Python, 
teach them Python.


> Back to the topic of variables. IMO the crucial factor that makes
> Python's variables ordinary, prosaic, programming language variables is
> the assignment. A real "non-variable" name-value binding would be
> permanent.

Ah, you're contrasting the idea of "variable" with "constant".


> Lisp without setq (or scheme without set!) would be such a
> language; in fact, the implementation would then not have anything
> resembling variables on the inside. It's the resettability that makes a
> variable a mundane memory slot, whether you have access to its address
> or not.

That does not make even a little sense. That's like saying that it's the 
presence of protein molecules in a hamburger that makes it soup.

The ability to rebind a name is not part of the definition of a memory 
slot. Memory slots are not necessarily rebindable -- a memory slot might 
be constant. Name bindings, whether variable or constant, may not have a 
fixed address. They might be key/value pairs in a dict, where the key is 
quite capable of being moved, as they are in Python -- dicts can be 
resized, even shrunk, and keys may move when they do so. In Jython, the 
entire dict can move in memory, since Java's memory management allows 
objects to be moved, unlike C-based implementations.

Or we might be talking about a language implemented in some exotic 
computing device, using clockwork, hydraulics, DNA recombination, or 
Conway's Game Of Life (which is Turing Complete), none of which have the 
concept of memory locations.

The fact that you seem to believe that *memory locations* are somehow 
fundamental to computing goes to show just how pernicious the concept is.


> (BTW, in lambda calculus and predicate logic, the names cannot be
> rebound, but they are still called variables.)
> 
> 
> Marko




[1] Not actually forced, of course, because rejecting the claim as 
"obviously wrong" without thought is possible. 


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



More information about the Python-list mailing list