
PYTHON — Basics of Python Inner Functions
Privacy is not something that we’re merely entitled to, it’s an absolute prerequisite. — Marlon Brando

PYTHON — Multiple Expressions in Lambda Functions in Python
## The Basics of Python Inner Functions
Python inner functions are functions defined inside other functions. They have access to the variables in the scope of their containing functions. In this article, we will explore inner functions through practical examples and discuss their primary use: closures.
Let’s start by looking at a simple example of an inner function.
def outer_function():
print("This is the outer function")
def inner_function():
print("This is the inner function")
inner_function()
outer_function()When you run the above code, you will see that the inner function is called from within the outer function, and both messages are printed.
Accessing Variables in the Containing Function’s Scope
Inner functions have access to the variables in the scope of their containing functions. Here’s an example to demonstrate this:
def mug(stuff):
def inside():
print(f"I like {stuff}")
inside()
mug("coffee")When mug("coffee") is called, it prints "I like coffee" because the inner function inside() has access to the variable stuff from the containing function mug().
Recursive Function Using Inner Function
A recursive function is one that calls itself. Here’s an example of a recursive function using an inner function to calculate a factorial:
def first_factorial(num):
def _recurse_factorial(n):
if not isinstance(n, int) or n < 0:
raise ValueError("Input must be a positive integer")
if n in (0, 1):
return 1
return n * _recurse_factorial(n - 1)
return _recurse_factorial(num)
result = first_factorial(4)
print(result) # Output: 24Refactoring Using Inner Function
You can refactor a function to use an inner function for encapsulation. Here’s a refactored version of the factorial function:
def second_factorial(num):
def _recurse_factorial(n):
if not isinstance(n, int) or n < 0:
raise ValueError("Input must be a positive integer")
if n in (0, 1):
return 1
return n * _recurse_factorial(n - 1)
return _recurse_factorial(num)
result = second_factorial(4)
print(result) # Output: 24Inner Functions vs. Private Helper Functions
Whether to use an inner function or a private helper function in the module is a matter of preference and reuse. If there’s a chance that the function could be used by any other function, it should be a private helper function. Here’s an example of refactoring to use a private helper function instead of an inner function:
def process_hotspots(file):
def _most_common_provider(file_obj):
# Implementation
pass
if isinstance(file, str):
with open(file, 'r') as f:
_most_common_provider(f)
else:
_most_common_provider(file)
# Call process_hotspots with a file name or file handleConclusion
In this article, we learned about Python inner functions and their use cases. You can use inner functions for encapsulation and to modularize your code. Understanding inner functions is essential for mastering closures in Python.
Now that you understand inner functions, you can move on to their primary use: closures.

