ID EN
Typing

Generic

Python 3.11 🇮🇩 Bahasa Indonesia

Kelas dasar abstrak untuk tipe generik.

Syntax

PYTHON
class typing.Generic

Contoh

Example 1
PYTHON
class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
         Etc.
Example 2
PYTHON
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