Re: The “does Python have variables?” debate (was: Pass variable by reference)

Chris Angelico rosuav at gmail.com
Tue May 6 20:11:01 EDT 2014


On Wed, May 7, 2014 at 9:55 AM, Ben Finney <ben at benfinney.id.au> wrote:
>> If you want to insist that Python has no variables, you will have to
>> also say that neither do Javascript, Ruby, Java, PHP, etc.
>
> I don't know enough Ruby to comment there. For Java, Javascript, PHP,
> they *do* have variables that work pretty much as expected by these
> newcomers: the variables are declared and exist before a value is
> assigned, they have the “box containing a value” conceptual model (as
> contrasted with Python's “value with sticky-notes attached” model), the
> “how do I pass by reference or by value?” question is meaningful — all
> of these are significantly different in Python, so the term “variable”
> is useful in those other languages but mostly just a confusing obstacle
> in learning Python.

JS "variables" are just name bindings. Imagine this change to the
Python parser: abolish the 'global' and 'nonlocal' keywords, and
instead declare 'local' anything that you want to be function-local.
(Always function-local. No actual change to Python byte-code.) Instead
of looking for assignment and declaring local on that basis, you look
for a 'local x' declaration. (Function parameters are, of course,
implicitly local, as in current model.) Now just rename "local" to
"var" and you have the JS scope model.

You can't pass by reference in JS; you pass objects around, exactly as
with Python. (With an exception with functions, where "x.y()" is
different from "f=x.y; f()", which strikes me as brain-dead design.)
You can have multiple names bound to the same object, and a mutation
in one is seen in the others. Rebinding a name disconnects it from the
previous binding. From what I can see, it's the same object model as
Python uses. And people are happy to talk about JS variables.

ChrisA



More information about the Python-list mailing list