Python Nested Functions
In Python, functions can be nested inside other functions. A nested function is only visible within the enclosing function. This allows us to create utility functions that are specific to a particular function and not needed elsewhere.
But why would we want to “hide” a function if it isn’t causing any harm? Well, it’s always good practice to hide functionality that is only relevant within a specific scope. Additionally, nested functions can make use of closures, which we’ll discuss later.
Here’s an example to illustrate nested functions:
1 | def talk(phrase): |
If you want to access a variable defined in the outer function from the inner function, you need to declare it as nonlocal
:
1 | def count(): |
Declaring the variable as nonlocal
is especially useful when working with closures, which we will explore in more detail later.
tags: [“Python”, “nested functions”, “closures”]