What are Python Closures?
A closure in Python is a nested or inner function that has access to variables within the scope of the enclosing (outer) function. In order to create a “closure” that lasts even after the outer function has returned, it “closes” over the variables it requires from the outer function.
Higher-order functions, or functions that interact with other functions, can only be created with the help of closures. Closures make it possible to create functions that are customized on-the-fly, without requiring additional arguments or complicated logic, by allowing inner functions to access the state of the outer function.
Although they can be a little difficult for beginners to grasp, Python closures are a crucial concept to understand if you want to write complex Python code. Spending some time learning about closures and how they function is a good idea if you’re just getting started with Python programming.
Looking to Learn Python? Book a Free Trial Lesson and match with top Python Tutors for concepts, projects and assignment help on Wiingy today!
Introduction to Closures in Python
Explanation of Closures:
In Python, a closure is a function that continues to have access to the variables in the scope of the enclosing function even after it has returned. When a nested function makes a reference to a value from the enclosing function, closures are created.
Understanding what, when & why to use closures!
If you want to write a function that can access state that endures across function calls, you should use closures. Iterators, generators, and other higher-order functions can be built using closures. They come in handy when you need to design a function that can be altered depending on an outside state.
Here’s an example:
1def make_adder(x):
2 def adder(y):
3 return x + y
4 return adder
5
6add5 = make_adder(5)
7add10 = make_adder(10)
8
9print(add5(3)) # Output: 8
10print(add10(3)) # Output: 13
In this example, make_adder returns an inner function, adder, which “closes over” the value of x. When we call make_adder(5), we get back a function that adds 5 to its argument. When we call make_adder(10), we get back a function that adds 10 to its argument. We can then use these functions to add 5 or 10 to any number we like.
Scoping Rules and Nested Functions in Python
Overview of Functions:
A function in Python is a section of code that completes a particular task. Functions can accept arguments as inputs and return results in the form of return values. The def keyword is used to define functions, and parentheses are used to call them.
Understanding Inner and Outer Functions:
Functions defined inside of other functions are known as inner functions. The variables in the outer function’s scope are accessible to them, but not the other way around. Functions defined outside of any other function are known as outer functions.
Python Nested Function:
A function that is defined inside another function is referred to as nested. Like inner functions, nested functions have access to the variables in the outer function’s scope. However, nested functions cannot be accessed from outside the outer function and are only defined when the outer function is called.
Here’s an example:
1def outer(x):
2 def inner(y):
3 return x + y
4 return inner
Use closure attribute:
In Python, you can use the closure attribute to check if a function is a closure, and to inspect its free variables (the variables it “closes over”). Here’s an example:
1/code start./ def make_counter():
2 count = 0
3 def counter():
4 nonlocal count
5 count += 1
6 return count
7 return counter
8
9c = make_counter()
10print(c()) # Output: 1
11print(c()) # Output: 2
12
13print(c.__closure__) # Output: (<cell at 0x7f9e3c3d6d10: int object at 0x7f9e3c4050c0>,)
14print(c.__closure__[0].cell_contents) # Output: 2
In this example, make_counter returns a closure that “closes over” the variable count. We can call this closure multiple times to get successive counts. We can also inspect the closure using the __closure__ attribute, which returns a tuple of cells that hold the free variables of the closure. In this case, the tuple contains a single cell that holds the value of count. We can access the value of count using the cell_contents attribute of the cell.
By understanding closures and nested functions, you can write more powerful, flexible Python code that takes advantage of the full range of Python’s features.
Looking to Learn Python? Book a Free Trial Lesson and match with top Python Tutors for concepts, projects and assignment help on Wiingy today!
FAQs
What are the closures in Python?
In Python, closures are functions that continue to have access to the variables in the scope of the enclosing function even after it has returned. When a nested function makes a reference to a value from the enclosing function, closures are created.
What is the difference between closures and decorators in Python?
While both decorators and closures are sophisticated Python concepts, they have different uses. Inner functions can access the state of the outer function thanks to closures, which establishes a “closure” that endures even after the outer function has finished and returned. On the other hand, decorators let you encase a function in another function to change its behavior.
What is the advantage of closures in Python?
The advantage of closures in Python is that they allow us to create functions that are customized on-the-fly, without requiring additional arguments or complex logic. By capturing the state of the enclosing function, closures can be used to create powerful, flexible functions that can be used in a wide range of situations.
What are the disadvantages of closures in Python?
One drawback of Python closures is that they can be challenging for beginners to understand. If closures are not used carefully, they may also cause memory leaks. Closures can also make debugging more challenging because they add an additional layer of scope.


Jan 30, 2025
Was this helpful?