Python Topics : Python Closures
A Basic Example of Python Closures
a closure is a powerful concept that allows a function to remember and access variables from its lexical scope
closures are closely related to nested functions
commonly used in functional programming, event handling and callbacks

a closure is created when a function (the inner function) is defined within another function (the outer function)
the inner function references variables from the outer function
closures are useful when you need a function to retain state across multiple calls without using global variables

def fun1(x):
  
    # This is the outer function that takes an argument 'x'
    def fun2(y):
      
        # This is the inner function that takes an argument 'y'
        return x + y  # 'x' is captured from the outer function
    
    return fun2  # Returning the inner function as a closure

# Create a closure by calling outer_function
closure = fun1(10)

# can use the closure, which "remembers" the value of 'x' as 10
print(closure(5)) 
Outer Function (fun1) fun1() takes an argument x and defines the fun2
fun2() uses x and another argument y to perform a calculation.
Inner Function (fun2) function is returned by fun1 and is thus a closure. It 'remembers' the value of x even after fun1has finished executing.
Creating and Using the Closure when fun1(10) is called, it returns fun2 with x set to 10
the returned fun2(closure) is stored in the variable closure
when closure(5) is called, it uses the remembered value of x (which is 10) and the passed argument y (which is 5), calculating the sum 10 + 5 = 15
Example of Python closure
def fun(a):
    # Outer function that remembers the value of 'a'
    def adder(b):
        # Inner function that adds 'b' to 'a'
        return a + b
    return adder  # Returns the closure

# Create a closure that adds 10 to any number
val = fun(10)

# Use the closure
print(val(5))  
print(val(20))
fun(10) creates a closure that remembers the value 10 and adds it to any number passed to the closure
How Closures Work Internally?
when a closure is created, Python internally stores a reference to the environment (variables in the enclosing scope) where the closure was defined
allows the inner function to access those variables even after the outer function has completed

a closure 'captures' the values from its surrounding scope and retains them for later use
allows closures to remember values from their environment

Use of Closures
Encapsulation closures help encapsulate functionality
the inner function can access variables from the outer function
those variables remain hidden from the outside world
State Retention closures can retain state across multiple function calls
especially useful in situations like
  • counters
  • accumulators
  • creating a function factory that generates functions with different behaviors
Functional Programming closures are a core feature of functional programming
allow creation of more flexible and modular code by generating new behavior dynamically
index