typing.Generic

class typing.Generic

Abstract base class for generic types.

A generic type is typically declared by inheriting from an instantiation of this class with one or more type variables. For example, a generic mapping type might be defined as:

1
2
3
4
class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
        # Etc.

This class can then be used as follows:

1
2
3
4
5
6
7
8
X = TypeVar('X')
Y = TypeVar('Y')
 
def lookup_name(mapping: Mapping[X, Y], key: X, default: Y) -> Y:
    try:
        return mapping[key]
    except KeyError:
        return default
doc_python
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.