On this page
article
Generic Function
Write type-safe generic functions.
Category: typing
Problem
Write type-safe generic functions.
Solution
T = TypeVar('T')
def first(items: list[T]) -> T: ...
Notes
- Adapt variable names and paths to your project
- Add error handling for production use
- See related chapters in the Learning Path