> I was under the impression Python virtually doesn't have lexical scoping at all and that's why `nonlocal` exists
Python has had lexical scoping since version 2.2. PEP 227 [0] "describes the addition of statically nested scoping (lexical scoping)" - allowing access to (but not assignment to) names in outer scopes.
`nonlocal` was introduced later, in Python 3.0 [1], specifically to allow assignment to names in outer scopes.
Python has had lexical scoping since version 2.2. PEP 227 [0] "describes the addition of statically nested scoping (lexical scoping)" - allowing access to (but not assignment to) names in outer scopes.
`nonlocal` was introduced later, in Python 3.0 [1], specifically to allow assignment to names in outer scopes.
[0] https://peps.python.org/pep-0227/
[1] https://peps.python.org/pep-3104/