What does it mean for functions to be first-class objects in Python?
First-class functions can be stored in variables, passed as arguments, returned from other functions, and placed in data structures — just like any other object. This is the foundation for higher-order functions, decorators, callbacks, and functional programming patterns in Python.
How to think about it
What this really means
“First-class” just means functions are values — full objects you can pass around like integers or strings. In Python every function is an instance of the function type and carries attributes (__name__, __doc__, __annotations__, __closure__). Understanding this unlocks decorators, callbacks, and functional patterns.
Four things you can do with a first-class function
- Store in a variable —
op = square - Store in a data structure — dispatch tables, pipelines
- Pass as an argument — higher-order functions like
sorted(key=func) - Return from a function — factory functions, decorators
Practical patterns that rely on first-class functions
sorted(items, key=func)— pass a function as the sort criterion.functools.partial— fix some arguments and return a new callable.- Dependency injection — pass behaviour rather than inheriting it.
- Dispatch tables — a dict of callables instead of long
if/elifchains.
The key insight
Because functions are objects, all the patterns above require zero special syntax. The language doesn’t need a special “callback keyword” or “strategy object” concept — you just pass a function. That’s the payoff of first-class status.