Beyond Def: The Architecture of Python Callables
Overview of Pythonic Callables
While the def keyword serves as the bread and butter for developers, Python’s internal flexibility allows for several methods of function creation that vary from the highly practical to the technically absurd. Understanding these methods isn't just a curiosity; it reveals how CPython treats functions as first-class objects. By exploring these alternatives, you gain a deeper understanding of the Python object model and the mechanics of the interpreter.
Prerequisites

To effectively use these techniques, you should have a firm grasp of:
- Function Signatures: Handling arguments and return types.
- Object-Oriented Programming: Understanding how classes and instances interact.
- Dunder Methods: Specifically how magic methods alter object behavior.
Key Libraries & Tools
- functools: A standard library for higher-order functions.
- types: Provides access to internal types like
FunctionTypeandCodeType. - typing: Used for defining protocols and type hinting.
Code Walkthrough
Partial Application and Lambdas
Python Lambda functions provide a concise way to create anonymous oneliners. However, for more robust configuration, functools.partial allows you to pre-fill function arguments.
from functools import partial
def power(base, exponent):
return base ** exponent
# Create a new function that always squares
square = partial(power, exponent=2)
print(square(5)) # Output: 25
The Callable Object Pattern
You can transform any class instance into a function by implementing the __call__ method. This allows the object to maintain internal state across multiple calls.
class Greeter:
def __call__(self, name: str):
return f"Hello, {name}!"
# Instance behaves like a function
say_hello = Greeter()
print(say_hello("Alice"))
Syntax Notes
Python functions are fundamentally objects. This means you can attach attributes directly to a function, such as my_func.metadata = "info". When using lambda, remember it is limited to a single expression and cannot contain statements like assert or pass.
Practical Examples
- Caching: Use decorators to wrap functions and store previous results.
- UI Callbacks: Use
partialto pass specific data to button click handlers without writing complex wrappers. - Plugin Systems: Use
execorevalto allow users to define logic in a dashboard, though you must sanitize inputs to prevent security breaches.
Tips & Gotchas
Directly manipulating bytecode via types.CodeType is extremely fragile. Python 3.11 introduced significant internal changes, including new resume opcodes and inline cache entries. Attempting to manually craft bytecode in newer versions will likely crash the interpreter unless you account for every specific internal requirement.
- Arjan
- 20%· people
- CPython
- 20%· products
- Python
- 20%· products
- Python 3.11
- 20%· products
- Python Lambda
- 20%· products

Writing Python Functions Like a Mad Scientist
WatchArjanCodes // 15:51
On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!