🪟 Closures¶
A closure is a function that remembers the variables from its outer (enclosing) scope, even after that outer function has finished executing.
✅ Why Closures Matter¶
- Let you create customized functions (function factories)
- Hide internal state without classes
- Enable patterns like decorators
✅ Example: Function Factory¶
Here, add remembers the value of n even after make_adder returns.
✅ Using nonlocal¶
To update an outer variable, use nonlocal.
def counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
next_count = counter()
next_count() # 1
next_count() # 2
🔍 Key Takeaways¶
- Closures capture values from enclosing scopes.
- They are useful for customization and encapsulation.
nonlocallets you update captured variables.