How can a Python function return a function?

In Python, functions are treated as first-class objects, which means all functions in Python are treated like any other object or variable. You can assign a function to a variable, pass it as an argument to another function, return it from a function, or even store it in data structures like lists.

One very useful feature in Python is that a function can return another function. This means you can define a function inside another function and return it as a result. This is used in advanced programming concepts like closures, decorators, and higher-order functions.

Syntax

Following is the basic pattern for a function returning another function −

def outer_function():
    def inner_function():
        # inner logic
        pass
    return inner_function   # return without (), so it's not called

result = outer_function()   # result now holds inner_function
result()                    # now call it

Note − When returning a function, use return inner_function (without parentheses). Writing return inner_function() would call the function and return its result instead.

Returning a Simple Function

You can define a function inside another function. The inner function can be returned by the outer function. This means you are not calling the inner function right away ? you are sending it back so that it can be used later.

Example

In this example, the outer() function creates inner() and returns it without calling it. We store the returned function in my_func and call it later −

def outer():
   def inner():
      print("Hello from the inner function!")
   return inner

my_func = outer()
my_func()

The output of the above code is −

Hello from the inner function!

Returning Functions Based on Conditions

You can return different inner functions based on some condition. The outer function decides which inner function to return depending on the input.

Example

Here, get_operation() returns different functions based on the operation name −

def get_operation(op):
   def add(a, b):
      return a + b
   def subtract(a, b):
      return a - b
   if op == "add":
      return add
   else:
      return subtract

f = get_operation("add")
print(f(10, 5))

g = get_operation("subtract")
print(g(10, 5))

The output of the above code is −

15
5

Using Closures

When a function returns another function and the inner function remembers the variables from the outer function, it is called a closure. The inner function can still use values from the outer function even after the outer function has finished executing.

Example

The outer function make_multiplier() returns a function that multiplies its input by a fixed number −

def make_multiplier(x):
   def multiplier(n):
      return x * n
   return multiplier

double = make_multiplier(2)
triple = make_multiplier(3)

print(double(5))
print(triple(5))

The output of the above code is −

10
15

Here, double remembers x=2 and triple remembers x=3 even though make_multiplier() has already finished executing. This is a closure.

Functions Returning Lambda Functions

You can also return lambda functions from another function. A lambda is a small anonymous function defined in one line, useful when the returned function is simple −

Example

def power_function(exp):
   return lambda x: x ** exp

square = power_function(2)
cube = power_function(3)

print(square(4))
print(cube(2))

The output of the above code is −

16
8

Using Decorators

Decorators are a special form of higher-order functions. A decorator takes another function and adds extra functionality to it without changing its original structure. Decorators use the @decorator_name syntax above the function they modify −

Example

def log_decorator(func):
   def wrapper(*args, **kwargs):
      print("Calling function:", func.__name__)
      result = func(*args, **kwargs)
      print("Function executed")
      return result
   return wrapper

@log_decorator
def greet(name):
   print("Hello,", name)

greet("Alice")

The output of the above code is −

Calling function: greet
Hello, Alice
Function executed

Summary of Techniques

Technique Returns Use Case
Simple return Inner function Deferred execution
Conditional return One of multiple functions Strategy / routing pattern
Closure Function with captured variables Function factories, state
Lambda return Anonymous function Simple one-line operations
Decorator Wrapped function Logging, auth, modifying behavior

Conclusion

Python functions can return other functions because functions are first-class objects. This enables powerful patterns like closures (inner functions remembering outer variables), conditional function factories, lambda returns, and decorators. The key is to return the function without parentheses ? returning the function object itself rather than calling it.

Updated on: 2026-03-18T08:23:22+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements