[Python-ideas] Why does += trigger UnboundLocalError?

andrew cooke andrew at acooke.org
Wed Jun 1 14:29:10 CEST 2011


On Wed, Jun 01, 2011 at 07:48:19AM +0200, Georg Brandl wrote:
> Because x += y is equivalent to
> 
> x = x.__iadd__(y)
> 
> and therefore an assignment is going on here.  Therefore, it's only logical to
> treat it as such when determining scopes.
> 
> Georg


And it's like this so that immutable classes work correctly, as far as I can
see.

So one way to answer the original idea is: because of immutable classes, +=
does not have the same semantics as .append()

Andrew



More information about the Python-ideas mailing list