Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, local functions (also known as nested functions) are functions defined within other functions. They can be used to create closures and for organizing your code. The nonlocal
keyword in Python allows you to access and modify variables from the enclosing scope within a nested function. Here's a tutorial on local functions and the nonlocal
keyword in Python:
Local functions are defined within the scope of another function, using the def
keyword:
def outer_function(): def inner_function(): # function body
Local functions can be called within the scope of the enclosing function:
def outer_function(): def inner_function(): print("Hello from the inner function!") inner_function() outer_function() # Output: Hello from the inner function!
Local functions can access variables from the enclosing scope, but can't modify them without using the nonlocal
keyword:
def outer_function(): outer_var = "I'm an outer variable" def inner_function(): print("Inside inner_function:", outer_var) inner_function() outer_function() # Output: Inside inner_function: I'm an outer variable
nonlocal
The nonlocal
keyword allows you to modify variables from the enclosing scope within a nested function:
def outer_function(): outer_var = "I'm an outer variable" def inner_function(): nonlocal outer_var # Use the nonlocal keyword to modify the outer_var outer_var = "I'm modified by the inner function" print("Inside inner_function:", outer_var) inner_function() print("Inside outer_function:", outer_var) outer_function()
Output:
Inside inner_function: I'm modified by the inner function Inside outer_function: I'm modified by the inner function
In this example, the inner_function
modifies the outer_var
using the nonlocal
keyword. This allows the outer_var
to be updated within the outer_function
scope as well.
A closure is a nested function that captures and remembers the values of the variables in the enclosing scope, even after the outer function has completed execution:
def make_multiplier(factor): def multiplier(n): return n * factor return multiplier times_two = make_multiplier(2) times_three = make_multiplier(3) print(times_two(4)) # Output: 8 print(times_three(4)) # Output: 12
In this example, the make_multiplier
function returns a nested function multiplier
, which captures the value of the factor
variable. The times_two
and times_three
functions are created as closures, each with their own captured value of factor
.
In summary, local functions in Python are functions defined within other functions. They can access and modify variables from the enclosing scope using the nonlocal
keyword, and can be used to create closures. Local functions are useful for organizing your code and for creating function factories or decorators.
How to define local functions in Python:
def outer_function(): def local_function(): print("This is a local function.") local_function()
Scope and lifetime of local functions in Python:
def outer_function(): def local_function(): print("Local function inside outer function.") local_function()
Accessing variables with the nonlocal keyword in Python:
nonlocal
keyword to access and modify variables in the enclosing (non-global) scope.def outer_function(): x = 10 def local_function(): nonlocal x x += 5 print(f"Modified x: {x}") local_function()
Nested functions and nonlocal variables in Python:
def outer_function(): x = 10 def inner_function(): nonlocal x x *= 2 print(f"Modified x: {x}") inner_function()
Using nonlocal to modify variables in outer scopes in Python:
nonlocal
keyword.def outer_function(): x = 10 def local_function(): nonlocal x x += 5 print(f"Modified x: {x}") return local_function
Dynamic behavior of local functions with nonlocal:
nonlocal
provide dynamic behavior in different contexts.def outer_function(base): def local_function(x): nonlocal base base += x return base return local_function
Type hints and local functions in Python:
def outer_function() -> None: def local_function() -> int: return 42 local_function()
Scope resolution in Python functions with nonlocal:
nonlocal
.def outer_function(): x = 10 def local_function(): nonlocal x print(x) local_function()