Python Closures (original) (raw)

Last Updated : 22 Aug, 2025

Closures in Python are like “memory-equipped” functions. They allow a function to remember values from the environment in which it was created even if that environment no longer exists. Closures are used in functional programming, event handling and callback functions where you need to retain some state without using global variables.

How Closures are Created

A closure is formed when:

Examples of Closures

Let's break down examples to understand how closures work.

**Example 1: This code demonstrates a Python closure, where inner function retains outer function’s variable even after outer function has finished executing.

Python `

def outer_function(x): # Outer function: takes 'x' and defines inner_function def inner_function(y): return x + y # 'x' is remembered from outer_function return inner_function # Returns inner function (closure)

Create a closure with x = 10

closure = outer_function(10)

Call the closure with different values of 'y'

print(closure(5)) print(closure(20))

`

**Explanation:

**Example 2: This code demonstrates a closure that maintains a running counter by remembering and updating a variable from its outer scope.

Python `

def make_counter(): count = 0 # This variable will be remembered def counter(): nonlocal count # Modify outer variable count += 1 return count return counter

counter1 = make_counter() print(counter1()) # 1 print(counter1())

`

**Explanation:

**Example 3: This example shows how a closure can be used for string formatting, outer function stores a prefix and inner function automatically attaches that prefix to any given text.

Python `

def pre(p): # Outer function stores prefix def add(t): # Inner function uses stored prefix return p + " " + t return add # Return closure

Create a closure that always prefixes with "Hello"

h = pre("Hello") print(h("World")) print(h("Python"))

`

Output

Hello World Hello Python

**Explanation:

How Closures Work Internally?

When Python creates a closure:

**Example: This code shows how closures can create custom multiplier functions by remembering **factor value from their enclosing scope.

Python `

def make_multiplier(factor): def multiply_by(x): return x * factor return multiply_by

double = make_multiplier(2) print(double(5))

Check closure variables

print(double.closure[0].cell_contents)

`

**Explanation:

Common Use Cases