Enclosing function
An enclosing function is a function whose body contains a nested function definition and provides a surrounding lexical scope for it.
Python Workout, Second Edition
An enclosing function is a function whose scope contains the definition or execution context of a nested function.
- How it is used
- Uses nested functions to retain invocation-specific configuration and state in returned callable objects.
- How deep it goes
- Explains nested-function creation, LEGB lookup, and nonlocal updates, stopping short of broader closure internals.
- Examples
- Demonstrates password generators, multiplication factories, call counters, shadowed built-ins, and local-versus-global names.
- What you can do after
- Build it from scratch using nested functions that retain configuration or update enclosing state with nonlocal.
Read how Python Workout, Second Edition explains enclosing function →
Python Workout: 50 ten-minute exercises
An enclosing function is a lexically surrounding function whose local bindings can be accessed by a nested function.
- How it is used
- Uses nested functions to return configurable behavior that retains values from an earlier function invocation.
- How deep it goes
- Explains LEGB lookup, closure creation, separate retained bindings, and nonlocal updates through runnable examples.
- Examples
- Demonstrates password generators, a returned multiplier, a persistent call counter, and shadowed names.
- What you can do after
- Apply it via Python to build returned functions that retain configuration or mutable state across calls.
Read how Python Workout: 50 ten-minute exercises explains enclosing function →
Also covered in
What to know first
- function declarationA function declaration is a source-language construct that introduces a function by specifying its name and parameter interface, and may also provide its implementation.
- password authenticationPassword authentication is a method of verifying a claimed identity or access by checking a supplied secret password, often alongside a user identifier.
- scopeScope is the region of a program in which names are visible and can be resolved to their bindings according to the language’s rules.
Related concepts
- functionkind of · A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- inner functionhas part · An inner function is a function defined within the body or scope of another function.
- nonlocal statementconfigured by · A nonlocal statement makes a name assigned in a nested Python function refer to a binding in an enclosing function scope.
- LEGBused by · LEGB is Python’s name-resolution order of local, enclosing-function, global, and built-in scopes.
- closureused by · A closure is a function together with access to bindings from the lexical environment in which it was created.
- scopepart of · Scope is the region of a program in which names are visible and can be resolved to their bindings according to the language’s rules.
Questions about enclosing function
What is enclosing function?
An enclosing function is a function whose body contains a nested function definition and provides a surrounding lexical scope for it.
Which Manning books explain enclosing function?
Python Workout, Second Edition and Python Workout: 50 ten-minute exercises.
What should I know before learning enclosing function?
Manning books that teach enclosing function usually introduce function declaration, password authentication and scope first.
What is enclosing function a kind of?
Enclosing function is a kind of function. A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
What is enclosing function part of?
Enclosing function is part of scope. Scope is the region of a program in which names are visible and can be resolved to their bindings according to the language’s rules.
What are the parts of enclosing function?
Enclosing function includes inner function.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

