Back to Study Material Python

Inner Function

A function defined inside another function is called an inner function, nested function, or local function. It is only accessible within the enclosing func...

Inner Function

Defining Functions Inside Functions

A function defined inside another function is called an inner function, nested function, or local function. It is only accessible within the enclosing function; the outer scope cannot call it directly. def outer(): def inner(): print("I am inner"); inner(). Inner functions are useful for organising code, hiding helper logic, and creating closures. They are created fresh on every call to the outer function, just like any other local variable.

Closures

A closure is an inner function that remembers variables from its enclosing scope even after the outer function has finished executing. def make_adder(n): def adder(x): return x + n; return adder. add5 = make_adder(5); add5(3) returns 8. The inner adder function closes over n. You can inspect what a closure captures with: add5.__closure__[0].cell_contents, which gives 5. Closures enable factory functions and are the mechanism behind decorators.

Why Use Inner Functions?

Inner functions serve several purposes. First, encapsulation: they hide implementation details that are only relevant to the outer function. Second, reuse within a function: if a block of code is repeated several times inside a function, extract it to an inner function for DRY code. Third, closures for state: an inner function can capture and maintain state across calls, as seen in counters. Fourth, decorators: Python decorators are implemented as functions returning inner functions.

Worked Example: Memoization

def memoize(func): cache = {}; def wrapper(n): if n not in cache: cache[n] = func(n); return cache[n]; return wrapper. @memoize; def slow_square(n): return n * n. The inner wrapper function closes over the cache dictionary, which persists across calls because it lives in the enclosing scope. Each unique argument is computed only once and stored. This manual memoizer mirrors what functools.lru_cache does internally.

Accessing Enclosing Variables

An inner function can read variables from the enclosing function without any special declaration. To modify them, use nonlocal. def counter(): count = 0; def increment(): nonlocal count; count += 1; return count; return increment. Each call to increment() increases count by 1 and returns the new total. Without nonlocal, the assignment count += 1 would create a new local variable in increment, leaving the outer count unchanged and causing UnboundLocalError.

Exam Tip

The most tested inner function concept is closures and nonlocal. A common question: given def f(): x = 0; def g(): x += 1; g(); print(x), what error is raised? UnboundLocalError because x += 1 treats x as local without nonlocal. The fix is nonlocal x. Another question: can an inner function be called from outside the outer function? Only if the outer function returns it or stores it somewhere accessible. The inner function itself remains private to the outer function's scope.