Nested scopes, and augmented assignment

Piet van Oostrum piet at cs.uu.nl
Sun Jul 9 14:48:19 EDT 2006


>>>>> Antoon Pardon <apardon at forel.vub.ac.be> (AP) wrote:

>AP> When someone gets confused over the difference between rebinding or
>AP> mutating a variable on an intermediate scope, the explanation he
>AP> mostly seems to get boils down to: one is rebinding, the other is
>AP> mutation, this is a fundametal difference in python.

>AP> My impression is that they seem to say that the fundamental difference
>AP> between mutation and rebinding implies the specific behaviour python
>AP> has now. IMO this explanation is incomplete. The python developers
>AP> could have chosen that a line like 'c.a = ...' would have resulted
>AP> in c being included in the local scope. Then rebinding and mutation
>AP> would still be fundamentally different from each other but the specific
>AP> confusion over why 'k[0] = ...' worked as expeced but 'k = ...' didn't,
>AP> will disappear.

That seems nonsense to me. If they had chosen that 'c.a = ...' would imply
that c would become a local variable, what would the value of c have to be
then, if there was no prior direct assignment to c? Would it become a new
binding like in 'c = ...'? From what class should it become an instance? Or
would it become a copy of the value that 'c' has in an outer scope? I don't
know any programming language where an assignment to c.a does create a new
c, rather than modifying the existing value of c. That would have been a
very strange language design. Similarly for 'k[0] = ...'. What would happen
with the other elements of k?

There are no situations in Python where an assignment to c.a or c[0]
suddenly lets spring c into existence. You always need an already existing
binding for c for this to be valid. And it always uses that binding, and
doesn't move or copy it to a different block.

Some of the confusing originates from the fact that assignment in Python is
subtly different from assignment in other programming languages. In most
languages variables denote memory locations or collections of memory
locations. An assignment then means changing the contents of those memory
locations. In python variables are bound to values, and assignment means
(re)binding the name to a possibly different value. With an example: most
other languages have boxes with the name of the variable on them. An
assignment changes the contents of the box. In Python you have values
(objects) and an assignment means sticking a label with the name of the
variable on the object. Often the difference is unnoticable, but there are
subtle cases where this really makes a difference. 

When the lefthandside of an assignment is an object attribute, subscription
or slice then the assignment is syntactic sugar for a mutation operation
implemented by the object, which usually changes the value of the object
but could do something completely different.

(Finally, the lefthandside can also be a [nested] tuple or list, in which
case it is a collection of parallel assignments. And oh yes, there are also
other binding operations e.g. a function or class definition.)
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list