-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
The cheat sheet from the docs shows the following:
# Mapping describes a dict-like object (with "__getitem__") that we won't
# mutate, and MutableMapping one (with "__setitem__") that we might
def f(my_dict: Mapping[int, str]) -> List[int]:
return list(my_dict.keys())
f({3: 'yes', 4: 'no'})
def f(my_mapping: MutableMapping[int, str]) -> Set[str]:
my_mapping[5] = 'maybe'
return set(my_mapping.values())
f({3: 'yes', 4: 'no'})
In both cases, the argument passed to f
is a dict
, though in one case the argument is annotated as Mapping
and in the other MutableMapping
.
From this example, it might not be clear to a beginner when to use Mapping
over MutableMapping
.