What are Python Closures
A closure is a function object that remembers values in the enclosing scope even if they are not present in memory. In other words, a closure allows the inner function to access variables defined in the outer function even after the outer function has finished running.
For example :
Python
def multiply_by(x):
def multiply(y):
return x * y
return multiply
multiply_by_5 = multiply_by(5)
result = multiply_by_5(3)
print(result)

You can also try this code with Online Python Compiler
Run Code
Output
15
In this example, `multiply_by` is the outer function that takes an argument `x`. Inside `multiply_by`, we define another function called `multiply` that takes an argument `y`. The `multiply` function multiplies `x` and `y` and returns the result.
When we call `multiply_by(5)`, it returns the `multiply` function with `x` set to 5. We assign this returned function to the variable `multiply_by_5`. Now, `multiply_by_5` is a closure that remembers the value of `x` as 5.
We can then call `multiply_by_5(3)`, which executes the `multiply` function with `y` set to 3. The closure remembers that `x` is 5, so it multiplies 5 by 3 and returns the result, which is 15.
Closures are powerful because they allow you to create function templates that can be customized with different values. This can make your code more reusable and easier to read.
When and Why to Use Closures
Closures are useful in several situations. Here are a few common scenarios where closures can be helpful:
Creating function factories
Closures allow you to create functions with customized behavior based on the arguments passed to the outer function. This is useful when you need to create many similar functions with slight variations in their behavior.
Example:
Python
def make_greeter(greeting):
def greet(name):
return f"{greeting}, {name}!"
return greet
hello_greeter = make_greeter("Hello")
hi_greeter = make_greeter("Hi")
print(hello_greeter("Rahul"))
print(hi_greeter("Rinki"))

You can also try this code with Online Python Compiler
Run Code
Output:
Hello, Rahul!
Hi, Rinki!
Encapsulating state
Closures can be used to encapsulate state within a function. This is useful when you want to create functions that maintain an internal state without using global variables.
Example:
Python
def counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
counter1 = counter()
counter2 = counter()
print(counter1())
print(counter1())
print(counter2())

You can also try this code with Online Python Compiler
Run Code
Output:
1
1
2
Implementing decorators
Decorators in Python are implemented using closures. Decorators allow you to modify or enhance the behavior of functions without changing their code.
Example:
Python
def uppercase_decorator(func):
def wrapper():
result = func()
return result.upper()
return wrapper
@uppercase_decorator
def greet():
return "hello, world!"
print(greet())

You can also try this code with Online Python Compiler
Run Code
Output:
HELLO, WORLD!
Closures are a powerful feature in Python that enable you to write more concise and expressive code. They are particularly useful when you need to create functions with customized behavior, encapsulate state, or implement decorators.
Frequently Asked Questions
What is a closure in Python?
A closure is a function object that remembers values in enclosing scopes even if they are not present in memory.
When should I use a closure?
Use closures to avoid the use of global variables and to implement data hiding and encapsulation in situations where objects are too heavyweight.
What makes a closure different from a plain function?
Unlike a plain function, a closure "closes over" the variables from its environment, maintaining access to these variables even after the outer function has finished executing.
Conclusion
In this article, we have learned the fundamentals of closures, how they preserve the execution environment, and their practical applications. We've discussed the nested functions as a way to implement closures and explained why the knowledge of closures can significantly enhance your programming skills in Python.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.