Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do nested functions work in Python?
In this article, we will explain nested/inner functions in Python and how they work with examples.
Nested (or inner) functions are functions defined within other functions that allow us to directly access the variables and names defined in the enclosing function. Nested functions can be used to create closures and decorators, among other things.
Defining an Inner/Nested Function
Simply use the def keyword to initialize another function within a function to define a nested function.
The following program demonstrates the inner function in Python −
Example
# creating an outer function
def outerFunc(sample_text):
sample_text = sample_text
# creating an inner function
def innerFunc():
# Printing the variable of the parent class(outer class)
print(sample_text)
# calling inner function
innerFunc()
# Calling the outer Function by passing some random name
outerFunc('Hello tutorialspoint python')
Hello tutorialspoint python
Here, innerFunc() is declared inside the outerFunc(), making it an inner function. We must first call outerFunc() before we can call innerFunc(). The outerFunc() will then call the innerFunc(), which is defined within it.
It is essential that the outer function be called so that the inner function can execute.
Without Calling Outer Function
# creating an outer function
def outerFunc(sample_text):
sample_text = sample_text
# creating an inner function
def innerFunc():
print(sample_text)
# calling inner function
innerFunc()
When run, the above code will return no results because the outer function is never called.
Scope of Variable in Nested Function
The scope of a variable is the location where we can find a variable and access it if necessary.
It is well-understood how to access a global variable within a function, but what about accessing the variable of an outside function?
The following program demonstrates the scope of the nested functions −
Example
# creating an outer function
def outerFunc():
sample_str = 'Hello tutorialspoint python'
# creating an inner function
def innerFunc():
print(sample_str)
# calling an inner function inside the outer function
innerFunc()
# calling outer function
outerFunc()
Hello tutorialspoint python
It is clear from the above example that it is equivalent to accessing a global variable from a function.
Modifying Variables from Outer Function
Assume you wish to modify the variable of the outer function.
The following program changes the value of a variable inside the nested function (inner function) −
# creating an outer function
def outerFunc():
sample_str = 'Hello tutorialspoint python'
# creating an inner function
def innerFunc():
# modifying the sample string
sample_str = 'Welcome'
print(sample_str)
# calling an inner function inside the outer function
innerFunc()
print(sample_str)
# calling outer function
outerFunc()
Welcome Hello tutorialspoint python
Here, the value of the outer function's variable remains unchanged. However, the value of the outer function's variable can be modified. There are several methods for changing the value of the outer function's variable.
Using an Iterable in Inner Functions
The following program demonstrates how to modify iterables in inner functions −
# Creating outer function
def outerFunc():
# Creating an iterable
sample_str = ['Hello tutorialspoint python']
# Creating inner Function/Nested Function
def innerFunc():
# using an iterable to modify the value
sample_str[0] = 'Welcome'
print(sample_str)
# Calling the inner function
innerFunc()
# Printing variable of the outer function
print(sample_str)
# Calling the outer function
outerFunc()
['Welcome'] ['Welcome']
Why Use Nested Functions?
Encapsulation and closures/factory functions are two of the most common reasons for using nested functions.
Data Encapsulation
There are cases when you wish to encapsulate a function or the data it has access to in order to prevent it from being accessed from other parts of your code.
When you nest a function like this, it becomes hidden to the global scope. Because of this characteristic, data encapsulation is also known as data hiding or data privacy.
# creating an outer function
def outerFunc():
print("This is outer function")
# creating an inner function
def innerFunc():
print("This is inner function")
# calling an inner function inside the outer function
innerFunc()
# trying to call inner function outside the outer function
try:
innerFunc()
except NameError as e:
print(f"Error: {e}")
Error: name 'innerFunc' is not defined
The inner function in the code above is only accessible from within the outer function. If you attempt to call inner from outside the function, you will see a NameError.
Instead, you must call the outer function properly −
# creating an outer function
def outerFunc():
print("This is outer function")
# creating an inner function
def innerFunc():
print("This is inner function")
# calling an inner function inside the outer function
innerFunc()
# calling outer function
outerFunc()
This is outer function This is inner function
Closures
But what if the outer function returns the inner function itself, rather than invoking it as in the previous example? In that instance, you'd have something called closure.
The conditions that must be met in order to build a closure in Python are as follows:
- A nested function is required
- The inner function must refer to a value that is defined in the enclosing scope
- The nested function must be returned by the enclosing function
Example
def number_1(a):
def number_2(b):
return a * b
return number_2
# Using the closure
multiply_by_3 = number_1(3)
result = multiply_by_3(2)
print(result)
# Or in one line
print(number_1(3)(2))
6 6
Closures allow you to pass data to inner functions without first passing it to outer functions with parameters. They also allow the inner function to be called from outside the encapsulating outer function. All of this with the previously mentioned benefit of data encapsulation/hiding.
Conclusion
Nested functions in Python provide powerful features like data encapsulation and closure creation. They help organize code by keeping related functionality together while maintaining proper scope isolation.
