Python Inner Functions

Introduction

Python inner functions, also known as nested functions, are functions defined within the scope of another function. Inner functions are particularly useful as helper functions, which encapsulate logic only useful within the context of another function, and as factory functions, which create and return other more specialized functions.

Advantages of Inner Functions

  1. Encapsulation: Inner functions allow you to encapsulate logic that is only relevant within the scope of the outer function. This can help keep your codebase organized and prevent polluting the global namespace with functions that are only meant to be used within a specific context.
  2. Code Modularity: Inner functions promote modularity by breaking down complex tasks into smaller, more manageable components. This makes your code easier to understand and maintain.
  3. Closures: Inner functions can access variables from their containing (outer) function, even after the outer function has completed its execution. This behavior is known as closure and can be quite powerful in certain scenarios.
  4. Improved Readability: When a function is defined within another function, it signifies to readers of your code that the inner function is closely related to and dependent on the outer function.

Defining Inner Functions

Defining an inner function is as simple as creating a regular function, but within the body of another function. Here’s the basic syntax:

def outer_function():
    # Outer function code
    
    def inner_function():
        # Inner function code
    
    # More outer function code

As you can see, the inner function, inner_function, is defined inside the scope of the outer function, outer_function, indicated by the indentation level.

Accessing Inner Functions

Inner functions can only be accessed and called from within the outer function. They are not available in the global scope or in other parts of your code that are outside the outer function’s scope. This isolation helps keep the inner function’s purpose clear and prevents unintended interactions with other parts of the code.

Example:

def outer_function():
    def say_hello():
       print("Hello")
    
    say_hello()

say_hello()

Output:

...
NameError: name 'say_hello' is not defined

In the example code above, the inner function say_hello is defined within outer_function. Trying to call it directly from the global scope results in an error.

Using Inner Functions

Consider an example where an outer function calculates the area of a rectangle and includes an inner function to validate the dimensions of the rectangle before doing the calculation.

Example:

def calculate_rectangle_area(length, width):
    def validate_dimension(dimension):
        if dimension <= 0:
            raise ValueError("Dimensions must be positive")
    
    validate_dimension(length)
    validate_dimension(width)
    
    area = length * width
    return area

In the example above, the validate_dimension() inner function checks whether a given dimension is positive. This logic is only relevant within the context of calculating the rectangle’s area, so it’s encapsulated within the calculate_rectangle_area() function.

Closures and Inner Functions

One of the powerful features of inner functions is their ability to create closures.

The term “closure” essentially means a function that is bundled up with external variables that existed at the time it was created. This allows an inner function to “remember” the variables from its containing (outer) function, even after the outer function has completed its execution.

Example:

def make_power_raiser(pow):
    def power_raiser(number):
        return number ** pow
        
    return power_raiser

square = make_power_raiser(2)
cube = make_power_raiser(3)

print(f"The sqwuare of 5 is {square(5)}.") 
print(f"The sqwuare of 7 is {square(7)}.") 
print(f"The cube of 5 is {cube(5)}.")  
print(f"The cube of 7 is {cube(7)}.")  

Output:

The sqwuare of 5 is 25.
The sqwuare of 7 is 49.
The cube of 5 is 125.
The cube of 7 is 343.

In this example, the outer function, make_power_raiser(), is capable of creating other functions that raise numbers to specified powers. The example uses it to create a square (line 7) and a cube (line 8) function.

The make_power_raiser function works by returning an inner function, power_raiser(), that captures the pow variable from the argument. The returned inner function acts as a closure, preserving the pow value when used later.

Factory Functions

Factory functions create and return other functions, and are a great use-case for inner functions. Factory functions often rely on closures.

In the previous example, make_power_raiser() is a factory function and as its name implies, it creates other functions that raise numbers to a specific power. It is useful in cases where you want to repeatedly raise numbers to specific powers without needing to repeatedly specify the power as an extra parameter.

To better understand the utility of factory function, let’s look at an alternative to the code in the previous example. This alternative does not use a factory.

Example:

def raise_to_power(number, pow):
    return number ** pow

print(f"The sqwuare of 5 is {raise_to_power(5, 2)}.") 
print(f"The sqwuare of 7 is {raise_to_power(7, 2)}.") 
print(f"The cube of 5 is {raise_to_power(5, 3)}.")  
print(f"The cube of 7 is {raise_to_power(7, 3)}.")  

Output:

The sqwuare of 5 is 25.
The sqwuare of 7 is 49.
The cube of 5 is 125.
The cube of 7 is 343.

The example above does not use a factory. Instead, it defined the raise_to_power() function, which has two parameters and simply raises one to the power of the other. (Of course, this is just a simple illustrative example, and in real code there is no need to write a function that raises to a power, since the existing Python power operator ** fine to use directly.) Because a factory is not involved, the power must be given as the second argument to raise_to_power() every time it’s called.

The factory in the previous example, once set up, provides cleaner code shortcuts. Let’s take a second look at it here.

Example:

def make_power_raiser(pow):
    def power_raiser(number):
        return number ** pow
        
    return power_raiser

square = make_power_raiser(2)
cube = make_power_raiser(3)

print(f"The sqwuare of 5 is {square(5)}.") 
print(f"The sqwuare of 7 is {square(7)}.") 
print(f"The cube of 5 is {cube(5)}.")  
print(f"The cube of 7 is {cube(7)}.")  

Limiting the Use of Inner Functions

In the first example in this lesson, the outer function calculate_rectangle_area() had an inner function, validate_dimension() that validated dimension inputs.

Now suppose you also write a calculate_square_area() function that similarly calculates the area of a square, and it also needs to validate its given dimension.

Example:

def calculate_square_area(side):
    def validate_dimension(dimension):
        if dimension <= 0:
            raise ValueError("Dimensions must be positive")
    
    validate_dimension(side)
    
    area = side * side
    return area

The calculate_square_area() function has the same inner function to validate a dimension as calculate_rectangle_area(). Consequently, it’s clear that validate_dimension() shouldn’t be an inner function because it does not uniquely belong to any specific outer function. It covers a more general capability that can be reused.

To enable its use by multiple functions validate_dimension() must taken out of the inner scope:

def validate_dimension(dimension):
    if dimension <= 0:
        raise ValueError("Dimensions must be positive")

def calculate_rectangle_area(length, width):
    validate_dimension(length)
    validate_dimension(width)    
    area = length * width
    return area

def calculate_square_area(side):
    validate_dimension(side)
    area = side * side
    return area

When creating an inner function, you should carefully consider whether it truly belongs in the inner scope by being a unique part of the outer function. Otherwise, a regular standalone function structure that is flatter and more general, as above, might be the better choice.

Disadvantages of Inner Functions

While inner functions offer various advantages, they also come with some limitations and potential downsides, as seen in the previous section. It’s important to consider these disadvantages when deciding whether to use inner functions in your code:

  1. Scope Complexity: Introducing inner functions creates an additional layer of scope within the outer function. This can make it harder to understand variable flow and lifetimes and may result in accidental variable overwrites or unintended modifications.
  2. Readability and Maintainability: Inner functions, especially when defined within lengthy or intricate outer functions, can reduce code readability. Poorly documented or inadequately named inner functions can degrade comprehension, making the codebase harder to maintain.
  3. Tight Coupling: Inner functions can lead to tight coupling between the inner and outer functions. If changes to the outer function affect the inner function’s behavior, it can increase code maintenance efforts and introduce fragility.
  4. Limited Reusability: Inner functions are closely tied to their containing outer function. Reusing an inner function in different parts of your code may necessitate code refactoring to extract the inner function into a standalone function, involving adjustments to variable passing.
  5. Debugging Complexity: Debugging code involving inner functions can be more intricate. The interactions between different scopes and variables introduced by inner functions can make identifying the root cause of an issue more challenging.

Summary & Reference for Python Inner Functions

Inner functions provide a powerful way to encapsulate logic within the scope of an outer function in Python.


Inner functions are defined within the body of an outer function using indentation.

def outer_function():
    def inner_function():
        # Inner function code

Inner functions are only accessible within the scope of the outer function, maintaining isolation. Trying to access them outside that scope results in an error.

def outer_function():
    def say_hello():
       print("Hello")
    
    say_hello()

say_hello()  # --> NameError

You can use inner function from within the body of the outer function.

def calculate_square_area(side):
    def validate_dimension(dimension):
        if dimension <= 0:
            raise ValueError("Dimensions must be positive")
    
    validate_dimension(side)  # Inner function call here.
    
    area = side * side
    return area

Inner functions can create closures, “remembering” variables from their creation time.

def make_power_raiser(pow):
    def power_raiser(number):
        return number ** pow
        
    return power_raiser

square = make_power_raiser(2)
cube = make_power_raiser(3)

print(square(5))  # --> 25
print(cube(5))  # --> 125

Factory functions create and return other functions, and are a great use-case for inner functions. In the previous example, make_power_raiser() is a factory function.


When creating an inner function, you should carefully consider whether it belongs in the inner scope by being a unique part of the outer function. Otherwise, a simpler and more general standalone function structure might be the better choice.