December 2018
Beginner to intermediate
796 pages
19h 54m
English
Going back to the preceding example, we can alter what happens to the shadowing of the test name by using one of these two special statements: global and nonlocal. As you can see from the previous example, when we define test = 2 in the inner function, we overwrite test neither in the outer function nor in the global scope. We can get read access to those names if we use them in a nested scope that doesn't define them, but we cannot modify them because, when we write an assignment instruction, we're actually defining a new name in the current scope.
How do we change this behavior? Well, we can use the nonlocal statement. According to the official documentation:
Read now
Unlock full access