3.3. Closures

3.3.1. Nested Function

  • Function inside the function

  • nested functions can access the variables of the enclosing scope

def outer(message):

    def inner():
        print(message)

    return inner

3.3.2. What is closure?

  • technique by which the data is attached to some code even after end of those other original functions is called as closures

  • Closures can avoid use of global variables and provides some form of data hiding

  • When the interpreter detects the dependency of inner nested function on the outer function, it stores or makes sure that the variables in which inner function depends on are available even if the outer function goes away

  • Method of binding data to a function without actually passing them as parameters is called closure

  • It is a function object obj that remembers values in enclosing scopes even if they are not present in memory

  • Closures provide some sort of data hiding as they are used as callback functions

  • This helps us to reduce the use of global variables

  • Useful for replacing hard-coded constants

  • Closures prove to be efficient way when we have few functions in our code

def outer(message):
    def inner():
        print(message)
    return inner


todo = outer('hello')
todo()
# hello

del outer
todo()
# hello
def f(x):
    def g(y):
        return x + y
    return g

3.3.3. Example

Listing 454. Functions has access to the outer scope
data = [1, 2, 3]

def add():
    print('inside', data)

add()
# inside [1, 2, 3]

print('outside', data)
# outside [1, 2, 3]
Listing 455. Variables in functions can shadow outer scope. Outer scope is restored, when function returns
data = [1,2,3]

def add():
    data = ['a', 'b', 'c']
    print('inside', data)

add()
# inside ['a', 'b', 'c']

print('outside', data)
# outside [1, 2, 3]
Listing 456. Functions can modify outer scope
data = [1,2,3]

def add():
    global data
    data = ['a', 'b', 'c']
    print('inside', data)

add()
# inside ['a', 'b', 'c']

print('outside', data)
# outside ['a', 'b', 'c']
Listing 457. inside function (closure) has access to its outer scope, that is outside function.
def outside():
    data = ['a', 'b', 'c']

    def inside():
        print('inside', data)

    inside()
    print('outside', data)

outside()
# inside ['a', 'b', 'c']
# outside ['a', 'b', 'c']
def outside():
    data = ['a', 'b', 'c']

    def inside():
        print('inside', data)

    return inside


my_ptr = outside()

print(my_ptr)
# <function outside.<locals>.inside at 0x11bfb8560>

my_ptr()
# inside ['a', 'b', 'c']
def outside():
    data = ['a', 'b', 'c']

    def inside():
        print('inside', data)

    return bbb


my_ptr = add()
del add

print(my_ptr)
# <function add.<locals>.bbb at 0x11bfb85f0>

my_ptr()
# inside ['a', 'b', 'c']