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
-- 
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to