Python Closures

    In this tutorial, we will discuss what is a python closure and how to create one. We will also discuss when to use it?

    Python Closures

    Nested function and non-local variable

    Before we discuss python, closure let’s discuss python nested function and non-local variable firsts. In our previous tutorial, we have discussed that what are nested loops, like a nested loop we have nested functions. A function defined inside another function is known as a nested function. A non-local variable is a free variable (Variable that not defined in that scope) used by the nested function.

    Note: We can not access the nested function in the global scope.

    Example:

    def outer_func():
        msg = "Hi local variable"
        def nested_func():
            print(msg)           # here msg become non-local variable
        return nested_func()      #returning nested function
    outer_func()

    Output:

    Hi local variable

    Behind the code

    In the above example we have defined two function one is outer_func() and the second is nested_func(), here nested_func() is defined inside the outer_func() . The outer_func is returning nested_func() , and when we are calling outer_func(), not only the outer_func() get execute it also execute the nested_func(). Here we can say that two functions are getting executed.

    Closure Definition

    In the above example, we have seen that every time if we want to execute the nested_func() we have to execute the outer_func(), this calling of two functions is not memory efficient. Here the closure property of python comes in play. With the help of closure property, we can directly access the nested function nested_func() without executing the outer_func() again and again.

    For example

    def outer_func():
        msg = "Hi local variable"
        def nested_func():
            print(msg)           # here msg become non-local variable      
        return nested_func         #returning nested function name
    call_nes = outer_func()    #setting call_nes = nested_func
    call_nes()
    call_nes()
    call_nes()

    Output:

    Hi local variable
    Hi local variable
    Hi local variable

    Behind the code

    In the above example instead of returning the complete nested function we are just returning function name, this time we have removed the parenthesis. When we call the outer function it return a function name and we have assigned that function name to call_nes . Now call_nes has become an alternate name of nested_func. Now with the help of call_nes(), we can call the nested_func() as many time as we want without executing the outer function again and again.

    Condition to be a closure.

    There are some conditions to satisfy the closure property of python.

    • The function must be nested
    • The nested function must have a variable not defined in its local scope
    • The function enclosing the nested function must return the name of the nested function.

    When to use Closures?

    Closures often use to replace the use of a class, because they provide data hiding.  In the upcoming tutorial, we will discuss python decorator a shorthand to implement python closure.