Default and Flexible Arguments in Python Functions – Data Science Best Practices 2026
Using default and flexible arguments effectively is a key skill for writing clean, reusable, and user-friendly functions in data science. In 2026, modern Python practices emphasize clear defaults, keyword-only arguments, and controlled flexibility.
TL;DR — Best Practices
- Use sensible default values for optional parameters
- Place default arguments after non-default ones
- Use keyword-only arguments (after
*) for important configuration options - Avoid mutable default arguments (lists, dicts, etc.)
1. Good Default Arguments Example
from typing import List, Optional
import pandas as pd
def preprocess_data(
df: pd.DataFrame,
target_column: str,
drop_na: bool = True, # Good default
scale_features: bool = False, # Sensible default
test_size: float = 0.2, # Common default in ML
random_state: Optional[int] = 42 # Reproducibility default
) -> dict:
"""
Preprocess data for machine learning.
"""
if drop_na:
df = df.dropna(subset=[target_column])
# Further preprocessing logic...
return {
"processed_df": df,
"test_size": test_size,
"random_state": random_state
}
2. Using Keyword-Only Arguments (Recommended for Configuration)
def train_model(
X_train,
y_train,
*,
model_type: str = "random_forest", # Keyword-only
n_estimators: int = 100, # Keyword-only
max_depth: Optional[int] = None, # Keyword-only
random_state: int = 42 # Keyword-only
):
"""
Train a model with clear configuration parameters.
Using * forces users to use keyword arguments for configuration.
"""
if model_type == "random_forest":
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
random_state=random_state
)
# ... other model types
model.fit(X_train, y_train)
return model
3. Avoiding Mutable Default Arguments (Important Gotcha)
# ❌ Bad practice - mutable default argument
def bad_function(data=[]): # Never do this!
data.append("item")
return data
# ✅ Correct way
def good_function(data=None):
if data is None:
data = []
data.append("item")
return data
4. Best Practices in 2026
- Provide sensible, commonly used default values
- Place all default parameters **after** non-default parameters
- Use **keyword-only arguments** (after `*`) for configuration options to improve clarity
- Never use mutable objects (lists, dicts, sets) as default values
- Use `Optional[...]` from typing when a parameter can be `None`
- Document default behavior clearly in the docstring
Conclusion
Default and flexible arguments, when used correctly, make your data science functions much more convenient and professional. In 2026, the standard is to combine sensible defaults, keyword-only parameters for configuration, and careful avoidance of mutable defaults. These practices result in functions that are easier to use, less error-prone, and more maintainable.
Next steps:
- Review your current data science functions and improve their parameter design using default values and keyword-only arguments where appropriate