Global vs Local Scope in Python – Best Practices for Data Science 2026
Understanding variable scope is crucial for writing clean, bug-free data science code. In 2026, following proper scoping rules helps prevent subtle bugs, improves code maintainability, and makes your functions more predictable and reusable.
TL;DR — Core Rules
- Local scope: Variables defined inside a function
- Global scope: Variables defined outside any function
- Functions can **read** global variables but cannot **modify** them without
globalkeyword - Avoid modifying global variables from inside functions
1. Basic Scope Example
total_sales = 0 # Global variable
def process_transaction(amount: float) -> float:
tax_rate = 0.08 # Local variable
total = amount * (1 + tax_rate)
# This would cause UnboundLocalError if we tried to modify global without 'global'
# total_sales += total # ❌ Wrong
return total
def update_total_sales(amount: float) -> None:
global total_sales # Explicitly declare we want to modify global
total_sales += amount
2. Common Data Science Scope Patterns (and Pitfalls)
def run_experiment(data):
model_name = "RandomForest" # Local variable
# Bad practice - modifying global inside function
# global results
# results.append(...)
# Better approach - return values instead
metrics = calculate_metrics(data, model_name)
return metrics
# Recommended pattern: Pass data explicitly, return results
def calculate_metrics(df: pd.DataFrame, model_name: str) -> dict:
"""Pure function - no side effects"""
# All variables here are local
accuracy = 0.92
f1_score = 0.89
return {
"model": model_name,
"accuracy": accuracy,
"f1_score": f1_score
}
3. Best Practices for Data Science in 2026
- Avoid global variables in functions whenever possible
- Pass all required data as parameters instead of relying on globals
- Use **pure functions** that return values instead of modifying global state
- Only use the
globalkeyword in very specific cases (like configuration or counters) - Keep functions self-contained and predictable
- Use dependency injection or configuration objects instead of global variables
Conclusion
Scope is one of the most important concepts in Python programming. In data science, following the principle of "explicit is better than implicit" means avoiding global variables in functions and preferring to pass data as parameters and return results explicitly. This approach leads to cleaner, more testable, and more maintainable code.
Next steps:
- Review your current data science scripts and replace any global variable modifications inside functions with proper parameter passing and return values