[Python-ideas] Why does += trigger UnboundLocalError?
Rob Cliffe
rob.cliffe at btinternet.com
Wed Jun 1 12:43:14 CEST 2011
> Yes, Jacob has got exactly what I was proposing. x += 1; x = 2 should
> continue to fail, since there would be a = statement in the function
> body in that case.
>
> -- Carl
My first reaction was: +1 on the proposed change. It seemed logical.
Then I had a reservation: it would widen the semantic difference between
x += 1
and
x = x + 1
which could trip someone innocently making a "trivial" code change from
the former to the latter (x unintentionally becomes a local).
So how about going further and say that x is only interpreted as local
if there is at least one NON-augmented assignment in which x appears as
a target on the LHS but x does NOT appear on the RHS?
I.e.
x = x + 1
(like "x += 1") does not (by itself) make x local.
Or is this getting too hard to explain?
Best wishes
Rob Cliffe
More information about the Python-ideas
mailing list