Using nonlocal in Nested Functions – Best Practices for Data Science 2026
The nonlocal keyword allows a nested (inner) function to modify a variable from its enclosing (outer) function’s scope. While not used as frequently as global, it is very useful in specific data science scenarios such as creating counters, accumulators, or maintaining state within nested helper functions.
TL;DR — When to Use nonlocal
- Use
nonlocalwhen a nested function needs to **modify** a variable defined in the enclosing function - Do **not** use it to modify global variables (use
globalfor that) - Most common use cases: counters, accumulators, and stateful nested functions
1. Basic Example of nonlocal
def process_batch(transactions):
total_revenue = 0.0 # Variable in enclosing scope
def add_transaction(amount: float):
nonlocal total_revenue # Allows inner function to modify outer variable
total_revenue += amount
for tx in transactions:
add_transaction(tx["amount"])
return total_revenue
# Usage
batch = [{"amount": 1250.75}, {"amount": 890.50}, {"amount": 2340.00}]
revenue = process_batch(batch)
print(f"Total revenue: {revenue}")
2. Real-World Data Science Example
def calculate_running_metrics(df):
"""Calculate running statistics using nested function with nonlocal."""
running_sum = 0.0
running_count = 0
def update_running_stats(amount: float):
nonlocal running_sum, running_count
running_sum += amount
running_count += 1
return running_sum / running_count if running_count > 0 else 0
df = df.copy()
df["running_avg"] = df["amount"].apply(update_running_stats)
return df
3. Best Practices in 2026
- Use
nonlocalonly when you need the inner function to **modify** a variable from the outer scope - Prefer returning values instead of using
nonlocalwhen possible (more functional style) - Limit the use of
nonlocalto small, well-contained nested functions - Document clearly when a nested function modifies outer variables using
nonlocal - Be cautious — overuse of
nonlocalcan make code harder to understand and debug
Conclusion
The nonlocal keyword is a specialized tool that allows nested functions to modify variables from their enclosing scope. In data science, it is most useful for creating accumulators, counters, or maintaining state within a parent function. However, in most cases, returning values explicitly is cleaner and more maintainable than relying on nonlocal.
Next steps:
- Review your current code and identify any nested functions that modify outer variables. Consider whether using
nonlocalis the best approach or if returning values would be cleaner.