Nested Functions in Python

A nested function means a function defined inside another function. It is also called an inner function. Nested functions are useful when we want to organize our code better, or create helper functions that are only needed inside the outer function.

Nested functions can also access variables from their outer function. This is called a closure.

Why use Nested Functions?

  • To keep helper functions hidden inside bigger functions.
  • Makes the code easier to read and manage.
  • Inner function can use variables from the outer function (closure).

Syntax:

def outer_function():
    # outer code
    def inner_function():
        # inner code
        ...
    inner_function()

Example 1: Simple nested function

def outer():
    x = 10
    def inner():
        print("Value of x is:", x)
    inner()

outer()

Output:

Value of x is: 10

Explanation:

  • inner() is defined inside outer().
  • It can access the x variable of outer().
  • We call inner() inside outer() to print the value.

Example 2: Returning inner function (closure)

def outer():
    message = "ShikshaSanchar is your learning partner!"
    def inner():
        print(message)
    return inner

my_func = outer()
my_func()

Output:

ShikshaSanchar is your learning partner!

Explanation:

  • outer() returns the inner function itself.
  • my_func now refers to inner.
  • Even after outer() finishes, inner() still remembers message.
  • This is called a closure.

Why use closures?

  • Closures allow inner functions to remember data from the outer function.
  • Very useful in scenarios like function factories (functions that return other functions).
  • Helps in keeping data secure inside the function, like private data.

Example 3: Trying to call inner function from outside

def outer():
    def inner():
        print("I'm inner")
    inner()

outer()
inner()  # Error!

Output:

I'm inner
Traceback (most recent call last):
NameError: name 'inner' is not defined

Explanation:

  • inner() works fine inside outer().
  • But outside outer(), Python does not know inner, so it gives a NameError.

Real-life analogy:

  • Think of outer() as a house and inner() as the kitchen.
  • You can cook (call inner()) only inside the house.
  • Outside the house, the kitchen does not exist.

Summary:

  • A nested function is defined inside another function.
  • It can access the variables of the outer function (closure).
  • It cannot be called directly from outside the outer function unless it is returned.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar