Journey
Interfaces
This journey shows how Python grows from simple functions to callable APIs, object interfaces, protocols, and metaclasses.
In this journey
- Start with functions as named behavior.
- Use functions as values.
- Bundle behavior with state.
Start with functions as named behavior.
Functions are the first abstraction boundary because they name behavior and control how callers provide information.
A function is the first abstraction boundary: arguments in, body, return value out.Use functions as values.
Python functions can capture state, be passed around, and wrap other functions.
Functions are first-class values. A second name binds to the same function object.- Global and Nonlocal
Use this example to control where assignment happens.
- Closures
Use this example to capture state in nested functions.
- Recursion
Use this example to solve self-similar problems with a base case.
- Lambdas
Use this example to write small unnamed functions for expression positions.
- Decorators
Use this example to wrap behavior without changing call sites.
- Partial Functions
Use this example to show how to pre-fill arguments with `functools.partial`.
Bundle behavior with state.
Classes become useful when data and behavior need to move together behind a stable interface.
Classes group fields and methods so data and behavior move together behind one interface.- Classes
Use this example to bundle state and behavior into a new object type.
- Inheritance and Super
Use this example to reuse and extend behavior through parent classes.
- Dataclasses
Use this example to generate common methods for data containers.
- Properties
Use this example to keep attribute syntax while adding computation or validation.
- Special Methods
Use this example to connect objects to Python syntax and built-ins.
- Truth and Size
Use this example to make objects work with truth tests and `len()`.
- Container Protocols
Use this example to support membership, lookup, and assignment syntax.
- Callable Objects
Use this example to make stateful instances callable like functions.
- Operator Overloading
Use this example to define operators only when the operation is unsurprising.
- Attribute Access
Use this example to customize fallback lookup and assignment carefully.
- Descriptors
Use this example to explain the protocol behind methods, properties, and managed attributes.
- Metaclasses
Use this example to customize class creation when ordinary class tools are not enough.