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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.