Consider this code:
def main():
l = []
def func():
l += [1]
func()
print(l)
if __name__ == '__main__':
main()
It will produce:
Traceback (most recent call last):
File "/Users/tahsmith/Library/Preferences/PyCharm2017.1/scratches/scratch_21.py", line 14, in <module>
main()
File "/Users/tahsmith/Library/Preferences/PyCharm2017.1/scratches/scratch_21.py", line 11, in main
func()
File "/Users/tahsmith/Library/Preferences/PyCharm2017.1/scratches/scratch_21.py", line 9, in func
l += [1]
UnboundLocalError: local variable 'l' referenced before assignment
This itself can be fixed by either using nonlocal l
at the start of func
or using __iadd__
directly instead of +=
.
Question: Why is nonlocal
needed here?
This is very surprising to me.