Python Inner Functions: What Are They Good For?
Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct access to variables and names defined in the enclosing function. Inner functions have many uses, most notably as closure factories and decorator functions.
In this tutorial, you’ll learn how to:
- Provide encapsulation and hide your functions from external access
- Write helper functions to facilitate code reuse
- Create closure factory functions that retain state between calls
- Code decorator functions to add behavior to existing functions
Creating Python Inner Functions
A function defined inside another function is known as an inner function or a nested function. In Python, this kind of function can access