[Python-ideas] Globalize lonely augmented assignment

Bruce Frederiksen dangyogi at gmail.com
Sat Jun 12 17:43:25 CEST 2010


On Sat, Jun 12, 2010 at 3:10 AM, Terry Reedy <tjreedy at udel.edu> wrote:
>
> On 6/11/2010 9:18 PM, Demur Rumed wrote:
>
>> I view the augmented assignment operators as different beasts.
>
> Your view is one that leads to buggy code. It is wrong in that respect.
> An augmented assignment STATEMEMT is both a STATEMENT, not an operator, and an ASSIGNMENT statement. Misunderstanding this leads to buggy code and posts on python list "why doesnt my code not work righ?"

I am curious about these buggy code examples.  Do you have any?

The standard assignment statement _binds_ the local variable.  But the
augmented assignment only _rebinds_ it.  The augmented assignment does
not give the variable a value if it doesn't already have one.

I think that we all agree that if the function has an assignment to
the variable some place else, the variable is a local variable.

So we are considering the case where no assignment to the variable
exists within the function, but there is an augmented assignment.  But
in this case, if we say that the variable is a local variable, how did
this local variable get a value that the augmented assignment can then
use?

The only way that I can think of is:

def foo():
    def bar():
        nonlocal A
        A = []
    bar()
    A += [2]

What am I missing?

-Bruce



More information about the Python-ideas mailing list