How do Python's global and nonlocal keywords work?

Gihin Oha
Member
Joined: 2025-02-16 08:26:47
2025-02-16 09:01:14

Hi all,
I’m a bit confused about Python's global and nonlocal keywords. I understand that global allows you to modify a variable from the global scope, but I’m not sure how nonlocal works or when I should use it. Can someone explain the difference between these two and provide examples of when each one is appropriate?

Thanks in advance!