Skip to content
Related Articles

Related Articles

Python Closures

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 08 Jun, 2022

Before seeing what a closure is, we have to first understand what nested functions and non-local variables are. 

Nested functions in Python

A function that is defined inside another function is known as a nested function. Nested functions are able to access variables of the enclosing scope. 
In Python, these non-local variables can be accessed only within their scope and not outside their scope. This can be illustrated by the following example: 


# Python program to illustrate
# nested functions
def outerFunction(text):
    text = text
    def innerFunction():
if __name__ == '__main__':

As we can see innerFunction() can easily be accessed inside the outerFunction body but not outside of its body. Hence, here, innerFunction() is treated as nested Function which uses text as non-local variable.

Python Closures

A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory. 

  • It is a record that stores a function together with an environment: a mapping associating each free variable of the function (variables that are used locally but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created.
  • A closure—unlike a plain function—allows the function to access those captured variables through the closure’s copies of their values or references, even when the function is invoked outside their scope.


# Python program to illustrate
# closures
def outerFunction(text):
    text = text
    def innerFunction():
    # Note we are returning function
    # WITHOUT parenthesis
    return innerFunction 
if __name__ == '__main__':
    myFunction = outerFunction('Hey!')

~/Documents/Python-Programs/$ python 
  1. As observed from the above code, closures help to invoke functions outside their scope.
  2. The function innerFunction has its scope only inside the outerFunction. But with the use of closures, we can easily extend its scope to invoke a function outside its scope.


# Python program to illustrate
# closures
import logging
def logger(func):
    def log_func(*args):
            'Running "{}" with arguments {}'.format(func.__name__,
    # Necessary for closure to
    # work (returning WITHOUT parenthesis)
    return log_func            
def add(x, y):
    return x+y
def sub(x, y):
    return x-y
add_logger = logger(add)
sub_logger = logger(sub)
add_logger(3, 3)
add_logger(4, 5)
sub_logger(10, 5)
sub_logger(20, 10)

~/Documents/Python-Programs/$ python 

When and why to use Closures:

1. As closures are used as callback functions, they provide some sort of data hiding. This helps us to reduce the use of global variables.

2.  When we have few functions in our code, closures prove to be an efficient way. But if we need to have many functions, then go for class (OOP).

This article is contributed by Omkar Pathak. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!