Python Glossary Русский

Python 3.5+typingterm

typing.Generic

generics and parameters

The base class for user-defined generic classes: class Box(Generic[T]).

Syntax

class C(typing.Generic[T]): ...

Since Python 3.12 you rarely inherit Generic[T] explicitly — the new class Box[T]: syntax (PEP 695) declares the parameter right in the header. Note the parameter is erased at runtime: Box[int] and Box[str] are the same class, with no type checks happening at run time.

Examples

from typing import Generic, TypeVar
T = TypeVar('T')
class Box(Generic[T]):
    pass
print(issubclass(Box, Generic))   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy