[Python-ideas] Why does += trigger UnboundLocalError?
Carl M. Johnson
cmjohnson.mailinglist at gmail.com
Wed Jun 1 08:48:55 CEST 2011
On Tue, May 31, 2011 at 7:48 PM, Georg Brandl <g.brandl at gmx.net> wrote:
> Because x += y is equivalent to
> x = x.__iadd__(y)
> and therefore an assignment is going on here. Therefore, it's only logical
> treat it as such when determining scopes.
But the difference is that you can only use += if the LHS name already
exists and is defined. So, it couldn't possibly be referring to a local name
if it's the only assignment-like statement within a function body. How could
it refer to a local if it has to refer to something that already exists?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas