
Shobha
The best way to predict the future is to invent it
What is a closure in Python?
A closure in Python occurs when an inner function retains access to variables from its outer function, even after the outer function has completed its execution. This ability for the inner function to "remember" and use variables from its original context is what makes a closure unique.
Example:
def outer_function(outer_variable):
def inner_function(inner_variable):
return outer_variable + inner_variable
return inner_function
closure = outer_function(10)
print(closure(5)) # Output: 15
Example:
def outer_function(outer_variable):
def inner_function(inner_variable):
return outer_variable + inner_variable
return inner_function
closure = outer_function(10)
print(closure(5)) # Output: 15