Register to get access to free programming courses with interactive exercises

Closures Python: Functions

Do you remember the inner function we created in the first lesson on FVP? So, this function was a closure.

A closure function is a function that accesses local variables and uses them in its body in the scope we created it in. It makes closures different from regular functions, which can use only their arguments and global variables.

Let us look at an example demonstrating a closure, and we will use it to show you what it is and how it works:

G = 10

def make_closure():
    a = 1
    b = 2
    def inner(x):
        return x + G + a
    return inner

In this example, inner is a closure:

  • The b variable is not in the body of inner and will not be remembered by the closure
  • However, the variable a is involved in generating the result of the inner call, and therefore we remember its value
  • The variable G is global if we accept the fact that the specified code is at the top module level and is not nested in any other blocks

By the way, functions created at the top module level, not inside other functions, are not closures. They do not lose anything because all external names visible to these functions are global. It works for imports and definitions from this module located higher in the code.

Memorizing the variable values

We need to mention an important feature: the actual memorization of the value occurs when the scope that the closure was created in ends, as it were, for example, when the current function has finished executing.

The easiest way to think of it is to imagine that when we create the function, we describe the closed variables like they should not forget to remember the current value of the variable xyz. These items execute the body of the function that created the closure has finished.

Here is an example demonstrating this latest memorization:

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.