Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is Closure? | Closure
Mastering Python: Closures and Decorators
course content

Course Content

Mastering Python: Closures and Decorators

Mastering Python: Closures and Decorators

1. Scopes
2. Closure
3. Decorators

bookWhat is Closure?

Note

The previous section (Scopes) is important to learn closure and decorators.

Earlier, we discussed the non-local scope. Now, let's learn how to convert it into an enclosing scope.

You know that function scope is created when the function is called and is deleted when the function ends execution:

123456789101112
def outer(): closed = 222 def inner(): print("Inner starts") print("Inner: non-local variable =", closed) print("Inner ends") inner() print("Outer ends") outer()
copy

The closed variable and inner() function are placed inside the outer() local scope.

Let's return the inner() function and assign it to the global variable:

12345678910111213
def outer(): print("Outer starts") closed = 222 def inner(): print("Inner starts") print("Inner: non-local variable =", closed) print("Inner ends") print("Outer ends") return inner variable = outer()
copy

The inner() function is not executed, but the outer() function ends executing and returns the inner() function.

Note

The inner() function in return written without parentheses (()) that means this function is not called.

To return the function, you should write just the function name (without calling).

A function is an object. You can assign the function to the variable:

12
variable = print variable("Let's print something via variable")
copy

Now pay attention to the outer() and inner() functions.
The outer() function returns the inner() function, which has a non-local variable:

1234567891011121314151617
def outer(): print("Outer starts") closed = 222 def inner(): print("Inner starts") print("Inner: non-local variable =", closed) print("Inner ends") print("Outer ends") return inner variable = outer() # variable = returned inner variable() # returned inner() variable() # returned inner() variable() # returned inner()
copy

In the example above, we can see that:

  1. The outer() function was executed once when the inner() function was returned and executed 3 times via variable.
  2. The outer() local scope is removed, but the inner() function has an enclosed non-local variable from the removed outer() local scope.
  3. The interpreter removed the whole outer() scope but left the closed variable from this scope because the inner() function has a reference to this variable.

This phenomenon is named closure.

Note

Enclosing scope is a removed non-local scope where objects can be left with references from other scopes.

When outer() is called, a new outer() local scope will be created that is independent of the previous one. Every returned inner() function is unique, has its enclosing scope, and is not dependent on other returned inner() functions.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
some-alt