[Python-ideas] Why does += trigger UnboundLocalError?
Terry Reedy
tjreedy at udel.edu
Wed Jun 1 19:41:26 CEST 2011
On 6/1/2011 12:52 AM, Carl M. Johnson wrote:
> So, my proposal is that += by itself should not cause x to be considered
> a local variable.
Right now, 'augmented assigment' is uniformly what it says: an
assignment with augmented behavior. 'expr1 op= expr2' is *defined* as
being the same as 'expr1 = expr1 op expr2' except that expr1 is
evauluated just once*, and if expr1 evaluates to a mutable, the op can
be done in place. Some consider the second exception to be a confusing
complication and a mistake. Your proposal would require a rewrite of the
definition and would add additional complication. Some would then want
another exception for when expr1 evaluates to a mutable within an
immutable (see Paul Svensson's post).
While I do understand your point, I also value uniformity.
-1
* It is actually more complicate than than. Expr1 is partially evaluated
just once to an internal reference rather than to an object. That
reference is then used once to fetch the existing object and once again
to rebind to the new or mutated object. Still, it is one behavior for
all occurrences.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list