Python

How to Use Nested Functions in Python

This article will cover a guide on using nested functions in Python. Nested functions or inner functions are defined inside other Python functions. They are useful in certain programming patterns and use cases. Some of them will be explained in this article. All code samples in this article are tested with Python 3.9.5 on Ubuntu 21.04.

About Nested / Inner Functions

Nested functions, as the name suggests, are Python functions that are created inside other Python functions. Besides its own scope, the inner function has access to the objects available in the scope of the outer function. The inner function can be termed as a single Python object with its own data and variables. This inner function is protected by the outer function and cannot be called or referred from the global scope. This way the inner function acts as a hidden entity that works within the boundaries of outer function only and global scope remains unaware of it. This process is also known as “encapsulation” in programming. Here is an example of a nested function in Python.

def visibile_outer_function(name):
    def  hidden_inner_function():
        print (name)
    hidden_inner_function()

visibile_outer_function("John")
hidden_inner_function()

The outer function takes one mandatory argument called “name”. The inner function has access to the scope of the outer function so it can make use of the name variable. A call to the inner function is then made in the outer function. Next, a call to both inner and outer functions is made in the global scope. After running the above code sample, you should get the following output:

John
Traceback (most recent call last):
  File "main.py", line 9, in
    hidden_inner_function()
NameError: name 'hidden_inner_function' is not defined

As you can see in the output, the outer function works fine when you call it from global scope. An error is thrown when you try to call the inner function as no such thing is available in the global scope.

Inner Functions Use Cases

Now that you have some understanding about nested functions, you may wonder about their utility and when to use them. One of the most common uses of inner functions is for creating helper functions within the main function. Inner functions can also be used as decorators and can be used to implement closures in your program. These use cases are explained below with examples.

Creating a Helper Function

Helper functions are like any other Python functions, but they are called “helper” functions because they can help better organize complex code and can be reused any number of times to avoid code repetition. Below is a code sample that illustrates an inner helper function.

def get_ticket_price(name):
    members = ["Tony", "Peter", "Mark"]
    price = 10

    def get_discounted_price(discount=1.0):
        return (price * discount)

    if name in members:
        ticket_price = get_discounted_price(discount=0.50)
    else:
        ticket_price = get_discounted_price()
    print ("Ticket price for " + name + " is: $" + str(ticket_price))

get_ticket_price("Tony")
get_ticket_price("John")

The main callable outer function is “get_ticket_price”. It takes the name of a person as the mandatory argument. The function “get_discounted_price” is an inner helper function that takes “discount” as an optional argument. The list “members” contains names of all registered members who are eligible for a discount. A discounted price for members is calculated by calling the inner function and supplying at it a discount value as an argument. This helper function can be called multiple times based on requirements and you can also change the logic within the inner function. Thus inner helper functions allow you to simplify code and avoid unnecessary repetition. After running the above code sample, you should get the following output:

Ticket price for Tony is: $5.0
Ticket price for John is: $10.0

As you can see in the output above, Tony gets a discount on ticket price as he is in the members list.

Implementing Closures

Closures are instances of inner functions that are returned by outer functions. These inner functions have access to the scope of outer functions and they continue to have access to the scope of outer function even after the outer function has stopped executing. Have a look at the code sample below:

def get_discounted_price(price):
    def discounted_price(discount):
        return price * discount
    return discounted_price

first_discount = get_discounted_price(10)
second_discount = get_discounted_price(10)
print (first_discount(0.50))
print (second_discount(0.60))

The outer function “get_discounted_price” returns a reference to the inner function called “discounted_price”. Notice that in the return statement, the function is called without braces. Next, two new instances called “first_discount” and “second_dicount” are created by calling the outer function and a value for “price” argument is supplied to these calls. At this point of time, the outer function has finished executing but its state has been saved in the first_discount and second_discount objects. Now when you call the first_discount and second_discount instances with braces and arguments, they will already have access to a variable called price along with its value. The argument supplied to these instances now goes to the inner function which then returns a result.

After running the above code sample, you should get the following output:

5.0
6.0

Closures are generally used in situations where your program requires preserving the state of a function.

Creating Decorating Functions

Decorator functions in Python modify behavior of an existing Python function without changing it. So when you attach a decorator to a function, you can add additional functionality to the function or modify its behavior while keeping its original behavior intact. A typical Python decorator looks like this:

@decorator
def decorated():
    pass

Here “@decorator” will modify the behaviour of the “decorated” function. You can create decorator functions using nested functions. To create a decorator, define a function and pass it to an outer function as an argument. This passed function is then called within another inner function where you can use it and implement logic. Finally the outer function returns the inner function which contains the modified behavior. Take a look at the code sample below.

def get_discounted_price(amount):
    def discounted_price():
        price = amount()
        new_price = price * 0.50
        return new_price
    return discounted_price

The outer function “get_discounted_price” is passed another function called “amount” as an argument. The inner function makes use of the passed function and adds a certain behavior to it. The outer function then returns a reference to the inner function that contains the modified behavior. After defining the decorator, you can call the it in following way:

@get_discounted_price
def get_price():
    return 10

print (get_price())

Decorators are attached to functions whose behavior you are trying to modify. They always start with the “@” symbol. By using the decorator here, you are passing the “get_price” function to the “get_discounted_price” function as an argument. Now when you call the get_price function, you won’t get 10 as output but a number modified by the get_discounted_price decorator. After running the above code sample, you should get the following output:

5.0

The decorator usage shown above is equivalent to the following code:

def get_discounted_price(amount):
    def discounted_price():
        price = amount()
        new_price = price * 0.50
        return new_price
    return discounted_price

def get_price():
    return 10

final_price = get_discounted_price(get_price)
print (final_price())

Instead of using a “@decorator” syntax as a shorthand, you can simply create a new instance of the outer function and supply it another function as an argument. End result of both coding patterns is the same. Since decorators keep the behavior of the original function intact, they are really useful if you want to call them on a case by case basis and at the same time preserve the vanilla implementation of a decorated function.

Conclusion

You can use nested functions in a variety of ways to create inner functions that add extra functionality and logic to the outer function. Some of the most common use cases for nested functions have been explained in the article. You can also create your own implementations of inner functions, as all functions are treated as first class objects in Python and they can be returned or passed as arguments.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.