Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Non-local Scope | Scopes
Mastering Python: Closures and Decorators
course content

Course Content

Mastering Python: Closures and Decorators

Non-local Scope

The non-local scopes are outer local scopes. This means that the non-local scope is placed between global scope and function local scope:

Let's look at the code example:

The first_outer() local and second_outer() local scope are non-local scopes for the inner() function.

Access to change non-local objects

You can change the non-local variable or another object using the nonlocal keyword (similar to the global keyword):

Note

Why pay attention to non-local scope?
The non-local scope is used for the closure. That's why non-local scope is also named enclosing scope. The closure will be described in the next section.

Everything was clear?

Section 1. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt