## Overview

## Python Nested Functions

**def**keyword inside another function to create a nested function.

- Scope
- Closure
- Encapsulation

**Scope**

The inner function can access variables from the outer function, but the reverse is not true. The outer function cannot access variables defined inside the inner function.

**Closure**

The inner function has access to variables and parameters of the outer function, creating a closure. This can be a powerful and flexible way to structure your code. The inner function forms a closure, capturing the variables from the outer function even after the outer function has finished execution. The inner function can still access outer function variables even after the outer function has returned.

**Encapsulation**

The main advantage of using nested functions is encapsulation. Nested functions can be used for encapsulation, allowing you to hide the implementation details of certain functionality within the scope of an outer function.

## Syntax

*def outer():*

*#outer function statements*

*def inner():*

*#inner function statements*

*inner() # inner function call*

*inner*function is defined within the scope of the

*outer*function. This means the

*inner*function is only in scope inside the

*outer*function.

## Example Program

*# Simple Program Nested Functions*

*# Python tutorials – www.TestingDocs.com*

*# Python outer() Function*

*def outer():*

* print(“This is outer()”)*

* # Python inner() Function*

* def inner():*

* print(“Control inside inner()”)*

* print(“outer() invokes inner() Function”)*

* inner()*

*outer()*

## Program Output

This is outer()

outer() invokes inner() Function

Control inside inner()

## Another Example

In this example, We will use Python nested functions

*# Python nested functions*

*# Python tutorials – www.TestingDocs.com*

*def rectangle_outer(length, width):*

* # This is the outer function*

* def calculate_area():*

* # inner function for calculating area*

* return length * width*

* def calculate_perimeter():*

* # inner function for calculating perimeter*

* return 2 * (length + width)*

* # Call the inner functions*

* area = calculate_area()*

* perimeter = calculate_perimeter()*

* # Return a dictionary with the calculated properties*

* return {“area”: area, “perimeter”: perimeter}*

*# Example usage*

*rectangle_dict = rectangle_outer(5, 8)*

*print(“Area:”, rectangle_dict[“area”])*

*print(“Perimeter:”, rectangle_dict[“perimeter”])*

In this example, rectangle_outer is the outer function that takes the length and width of a rectangle as parameters. Inside the outer function are two inner functions: calculate_area and calculate_perimeter. These inner functions have access to the length and width variables from the outer function.

The outer function then calls the inner functions to calculate the area and perimeter, and it returns a dictionary containing both values. This way, you can obtain a rectangle’s area and perimeter.

This example demonstrates how nested functions can encapsulate related functionality within a single function, providing a clean and organized way to structure your code.

—

## Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/

More information on Python is available at the official website: