Can there be a function inside a function in Python?

You don't really gain much by doing this, in fact it slows method_a down because it'll define and recompile the other function every time it's called. Given that, it would probably be better to just prefix the function name with underscore to indicate it's a private method -- i.e. _method_b.

I suppose you might want to do this if the nested function's definition varied each time for some reason, but that may indicate a flaw in your design. That said, there is a valid reason to do this to allow the nested function to use arguments that were passed to the outer function but not explicitly passed on to them, which sometimes occurs when writing function decorators, for example. It's what is being shown in the accepted answer although a decorator is not being defined or used.


Here's proof that nesting them is slower (using Python 3.6.1), although admittedly not by much in this trivial case:

setup = """
class Test(object):
    def separate(self, arg):
        some_data = self._method_b(arg)

    def _method_b(self, arg):
        return arg+1

    def nested(self, arg):

        def method_b2(self, arg):
            return arg+1

        some_data = method_b2(self, arg)

obj = Test()
from timeit import Timer
print(min(Timer(stmt='obj.separate(42)', setup=setup).repeat()))  # -> 0.24479823284461724
print(min(Timer(stmt='obj.nested(42)', setup=setup).repeat()))    # -> 0.26553459700452575

Note I added some self arguments to your sample functions to make them more like real methods (although method_b2 still isn't technically a method of the Test class). Also the nested function is actually called in that version, unlike yours.

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)


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:

Traceback (most recent call last):
  File "", line 9, in
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)
        ticket_price = get_discounted_price()
    print ("Ticket price for " + name + " is: $" + str(ticket_price))


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:


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:

def decorated():

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:

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:


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.


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.

Can a function be inside a function?

A nested function is a function that is completely contained within a parent function. Any function in a program file can include a nested function. The primary difference between nested functions and other types of functions is that they can access and modify variables that are defined in their parent functions.

Is it OK to have functions inside functions?

It's actually fine to declare one function inside another one. This is specially useful creating decorators. However, as a rule of thumb, if the function is complex (more than 10 lines) it might be a better idea to declare it on the module level.

How do you use a function inside a function?

Write one function inside another function..
Make a call to the inner function in the return statement of the outer function..
Call it fun(a)(b) where a is a parameter to the outer and b is to the inner function..
Finally, return the combined output from the nested function..

How do you pass a function inside a function in Python?

functions are first-class objects in python. you can pass them around, include them in dicts, lists, etc. Just don't include the parenthesis after the function name. Example, for a function named myfunction : myfunction means the function itself, myfunction() means to call the function and get its return value instead.