Skip to main content

How to Define a Function Inside Another Function in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A function can have its own small helper functions, defined right inside it with def. The helper exists only while the outer function runs, and it can read the outer function's variables without having them passed in. This guide writes one on Python 3.15.2.

Syntax​

Syntax
def <outer>(<parameters>):
def <inner>():
... # can read the outer function's variables

return <inner>()
PartWhat it means
def <inner>() inside <outer>Creates the helper each time <outer> runs. Only code inside <outer> can call it
Reading outer variables<inner> can use <outer>'s parameters and variables directly

The Python glossary describes it under "nested scope": "a function defined inside another function can refer to variables in the outer function."

The Example​

A report function describes a list of numbers. Inside it, a helper named total adds them up, reading numbers straight from report.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

inner_function.py
def report(numbers):
def total():
return sum(numbers)

return f"{len(numbers)} numbers, total {total()}"


print(report([2, 3, 5]))

The inner_function.py script in a macOS-style code window: a report function with an inner total function that returns sum(numbers), and report returning a sentence with the count and total(); the script prints report([2, 3, 5]).

total takes no parameters. It reads numbers from report.

Step 2: Run It​

Terminal
uv run python inner_function.py
Output
3 numbers, total 10

The terminal after uv run python inner_function.py: 3 numbers, total 10.

report called its own helper, and total() added up the numbers it was given. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Calling the helper from outside: print(total()) after reportNameError: name 'total' is not defined. The helper only exists inside reportCall it inside the outer function, or define it at the top level if other code needs it
Defining the helper after the line that calls itUnboundLocalError: cannot access local variable 'total' where it is not associated with a value, because def hasn't run yet when Python reaches the callPut the inner def before its first use

Troubleshooting​

SymptomLikely causeFix
NameError for the helper's nameIt's called outside the function that defines itMove the call inside, or move the helper out

FAQ​

Why not define the helper at the top level?​

You can. Put it inside when only one function uses it and it needs that function's variables: it keeps the helper next to its only user and saves passing values in.

Can the inner function change an outer variable?​

Reading works, but assigning a new value creates a separate local variable instead. The glossary notes that "nested scopes by default work only for reference and not for assignment". The nonlocal statement changes that.

Where do nested functions show up in real code?​

Decorators are built from them: the wrapper a decorator returns is a function defined inside the decorator.

Conclusion​

total, defined inside report, read numbers without parameters and returned 10, and calling it from outside gave a NameError. Define a helper inside a function when only that function needs it.

Additional Resources​