nonlocal or not
Rob Cliffe via Python-list <[email protected]>
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <[email protected]> |
Here is a trivial program (I am using Python 3.13.3 on Windows 11):
def main():
def sub():
nonlocal line
print(len(line))
line += 'x'
res.append('y')
line = ''
res = []
sub()
print(line, res)
main()
The output, as expected, is
0
x ['y']
What I don't understand is that in the sub function,
it is necessary to declare line nonlocal but it is NOT necessary to
declare res nonlocal.
Can someone please explain this?
Best wishes
Rob Cliffe
--
https://mail.python.org/mailman3//lists/python-list.python.org