[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
> to
> 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...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110531/518b247c/attachment.html>

More information about the Python-ideas mailing list