Slide 32
Slide 32 text
User-defined Generic types
Example: a generic mapping type
13 except KeyError: return default
32 / 57
1 from typing import TypeVar, Generic
2 KT, VT = TypeVar("KT"), TypeVar("VT")
3
4 class Mapping(Generic[KT, VT]):
5 def __getitem__(self, key: KT) -> VT: pass
6
7 X, Y = TypeVar("X"), TypeVar("Y")
8
9 def lookup_name( # Using Mapping
10 mapping: Mapping[X, Y], key: X, default: Y
11 ) -> Y:
12 try: return mapping[key]