mdw at distorted.org.uk
Sat Nov 13 21:01:42 CET 2010
Terry Reedy <tjreedy at udel.edu> writes:
> On 11/13/2010 11:29 AM, Mark Wooding wrote:
> > Alas, Python is actually slightly confusing here, since the same
> > notation `=' sometimes means assignment and sometimes means mutation.
> I disagree somewhat. An object is mutated by an internal assignment.
Some object types are primitive, provided by the runtime system; there
are no `internal' variables to be assigned in these cases. (It is
possible in principle in Python 3 to implement all of the compound data
types using only assignment and closures.)
> "ll = 1" assigns 1 to the 0 slot of ll.
> "o.a = 1" assigns 1 to the 'a' attribute of o.
> This which might be implemented by assigning 1 to the 'a' slot of o.__dict__,
> just as "a=1" might be implemented by assigning 1 to the 'a' slot of a
> namespace dict.
There's a qualitative difference here: simple assignment has semantics
defined by the language and provided by the implementation, and can
therefore be understood in isolation, using only lexically apparent
information; whereas the complex assignments are implemented by invoking
methods on the objects mentioned on the left hand side. The objects in
question may choose not to implement some other semantics, so the
runtime behaviour might not be determinable even in principle without
actually executing the program. (Compound assignment has the same
problem writ large.)
> Assignment *always* binds an object to a target.
No! Assignment /never/ binds. There is syntactic confusion here too,
since Python interprets a simple assignment in a function body -- in the
absence of a declaration such as `global' to the contrary -- as
indicating that the variable in question should be bound to a fresh
variable on entry to the function. But assignment itself doesn't
perform binding. (This is a persistent error in the Python community;
or, less charitably, the Python community gratuitously uses the word in
a different sense from the wider programming-language-theory community.
See Lisp literature passim, for example.)
There's a two step mapping: names -> storage locations -> values.
Binding affects the left hand part of the mapping; assignment affects
the right hand part.
More information about the Python-list