Functions as Arguments in Python 2026 – Best Practices for Writing Functions
Passing functions as arguments to other functions is one of the most powerful and commonly used patterns in Python. It enables higher-order functions, callbacks, decorators, strategy patterns, and much more flexible code.
TL;DR — Key Takeaways 2026
- You can pass any function as an argument just like any other object
- This pattern is the foundation of callbacks, event handlers, and strategy design
- Use
Callablefromtypingfor clear type hints - Keep passed functions pure when possible for predictable behavior
1. Basic Example
def greet(name: str) -> str:
return f"Hello, {name}!"
def shout(text: str) -> str:
return text.upper() + "!!!"
def process_message(message: str, formatter):
"""formatter is a function passed as argument."""
formatted = formatter(message)
print(formatted)
process_message("hello world", greet) # Hello, hello world
process_message("hello world", shout) # HELLO WORLD!!!
2. Real-World Higher-Order Function
from typing import Callable, List
def apply_to_all(items: List[int], operation: Callable[[int], int]) -> List[int]:
"""Apply any function to every item in the list."""
return [operation(item) for item in items]
def square(x: int) -> int:
return x * x
def double(x: int) -> int:
return x * 2
numbers = [1, 2, 3, 4, 5]
print(apply_to_all(numbers, square)) # [1, 4, 9, 16, 25]
print(apply_to_all(numbers, double)) # [2, 4, 6, 8, 10]
3. Best Practices in 2026
- Use descriptive parameter names (e.g., `processor`, `formatter`, `callback`)
- Add proper type hints with `Callable[[ArgTypes], ReturnType]`
- Document what arguments the passed function should accept and what it should return
- Prefer small, focused functions when passing them as arguments
- Combine with lists or dictionaries of functions for even more flexibility
Conclusion
Passing functions as arguments is a cornerstone of modern Python programming. In 2026, this technique allows you to write highly reusable, modular, and extensible code. From simple data processing to complex event-driven systems, functions as arguments unlock powerful design patterns.
Next steps:
- Look for repetitive code that can be replaced by passing different functions as arguments
- Related articles: Writing Functions in Python 2026 • Functions as Objects in Python 2026 • Lists and Dictionaries of Functions in Python 2026