Re: nonlocal or not
Chris Angelico via Python-list <[email protected]>
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <CAPTjJmr_w=vavjz0OoK37B975_PmmQA_jixrtQ6Z-gwrC0m2kA@mail.gmail.com> |
On Thu, 17 Sept 2026 at 20:44, Rob Cliffe via Python-list <[email protected]> wrote: > > 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? You're assigning to line, you're not assigning to res. ChrisA