Python Generic Type Parameters
Learn Python type parameter syntax for generic functions, classes, and type aliases, including type-parameter scope, bounds, defaults, and the roles of TypeVarTuple and ParamSpec.
Advanced
Python lets you make functions, classes, and type aliases generic by putting a type parameter list after their name. This gives one definition a type parameter such as T that can represent different types.
A generic function
Put a type parameter in square brackets immediately after the function name. The parameter can then be used in the function annotations.
|
|
The same function definition is generic over T, so T can represent the element type for different calls.
A generic class
A class can declare a type parameter in the same position. The parameter is then available in annotations inside the class body.
|
|
T is available throughout the generic class body, including method annotations.
A generic type alias
The type statement from the previous lesson can also have type parameters.
|
|
The alias is parameterized with int when it is used in an annotation.
Use more than one type parameter
Separate type parameters with commas when a result relates more than one type.
|
|
T describes the first value and U describes the second, so their types do not have to be the same.
Type parameters have their own scope
A type parameter belongs to the generic function, class, or alias that declares it. It is not automatically a name in the surrounding module scope.
|
|
The type parameter is attached to the generic object; T is not created as an ordinary module-level variable.
A type parameter can have a bound
A type variable can have a bound after a colon. The bound describes the types a type checker may allow for that parameter.
|
|
The bound is available on the type-parameter object. The syntax does not insert a runtime type check into the function.
Python 3.13+ also supports type-parameter defaults
A type parameter may have a default expression after =. This affects type-parameter substitution for type-checking, and the default is available from the type-parameter object.
|
|
The default is metadata on the type parameter; it does not convert the argument to int at runtime.
The other type-parameter kinds
Python also provides TypeVarTuple with a leading * and ParamSpec with a leading **. These support generic APIs involving a variable number of types or a callable’s parameter list. They are useful, but their practical typing patterns are more advanced than the single-type-parameter examples in this lesson.
|
|
A TypeVarTuple can represent a variable-length sequence of type parameters.
|
|
A ParamSpec can describe the parameter list of a callable while another type parameter describes its result.
Key idea
A type parameter list makes a function, class, or type alias generic. Start with a simple T when one type varies, add more parameters when types have different roles, and move to TypeVarTuple or ParamSpec only when the API genuinely needs their more advanced relationships.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.