[issue46612] Unclear behavior of += operator

Marek Scholle report at bugs.python.org
Wed Feb 2 11:49:00 EST 2022


Marek Scholle <mscholle at purestorage.com> added the comment:

I don't understand the comment https://bugs.python.org/issue46612#msg412374

----
>>> def f(): x
...
>>> f()
----
is OK, so x is something which can be evaluated inside nested function, it is a good target to be used in `x.__iadd__(iterable)`.

That 
----
>>> def f(): x = x + 1
...
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in f
UnboundLocalError: local variable 'x' referenced before assignment
----
is OK, the interpreter sees `x` as local variable (by default inner scope variables shadow those from outer scopes), hence the `UnboundLocalError`

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46612>
_______________________________________


More information about the Python-bugs-list mailing list