[TriZPUG] "Use of the Words 'Variable' and 'Assignment' Considered Harmful in Python"

David Handy david at handysoftware.com
Thu Sep 29 20:43:39 CEST 2011


> Tom Roche Thu, Sep 29, 2011 at 09:33:47AM -0400
> >> "Variable assignment" is a common informatic idiom, and it *seems*
> >> pretty similar to what's happening in
> 
> >> $ python -c 'i=1 ; print i'
> 
> > The potential harm is that it tends to communicate the wrong
> > conceptual model. This isn't like C or assembly language, where
> > there is a memory location labeled "i" and we're stuffing the
> > integer 1 in that memory location.
> 
> Correct, but that to me says less about the use of the term "variable
> assignment" (with which the parallelism to other languages is plain)
> and more about the differing implementation of variables. Python
> variables are implemented by objects managed by the VM; in other
> languages, variables map to memory locations managed by you.

Up to this point, what you described is essentially the same in both Java
and Python (ignoring that Java does have volatile integers and such, and
just concentrating on regular Java objects.)

> (Am I missing something?) That's a distinction which I don't see the term
> "binding a name to an object" necessarily communicating much better than
> "assigning a value to a variable"--YMMV.

The distinction is the dynamic nature of binding names to objects vs. the
static nature of declaring variables in Java and other languages.

You can't do this in Java:

>>> a = 1
>>> vars()
{'a': 1, '__builtins__': <module '__builtin__' (built-in)>, '__package__':
None, '__name__': '__main__', '__doc__': None}
>>> del a
>>> vars()
{'__builtins__': <module '__builtin__' (built-in)>, '__package__': None,
'__name__': '__main__', '__doc__': None}


Nor this:

>>> def f():
...     print "I'm a function!"
...
>>> a = 1
>>> a = f
>>> a()
I'm a function!

In Java the list of variables and fields can never change, and the type of
each one can never change.

In Python anything can change. You're just binding a name to an object in a
namespace. The name can always be unbound and disappear, or be rebound to
something totally different. You can add new names at runtime. Python names
are so dynamic that, ironically, they don't resemble those static,
unchanging things called "variables" in other languages. (Inigo Montoya:
"That word, I do not think it means what you think it means...")

David H


More information about the TriZPUG mailing list