Skip to content

Commit

Permalink
Update examples of mutable and non-mutable mapping in cheat sheet (#7620
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MarcoGorelli authored and Guido van Rossum committed Oct 9, 2019
1 parent 54e3452 commit 6f8480c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/source/cheat_sheet_py3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ that are common in idiomatic Python are standardized.
# 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]:
my_mapping[5] = 'maybe' # if we try this, mypy will throw an error...
return list(my_dict.keys())
f({3: 'yes', 4: 'no'})
def f(my_mapping: MutableMapping[int, str]) -> Set[str]:
my_mapping[5] = 'maybe'
my_mapping[5] = 'maybe' # ...but mypy is OK with this.
return set(my_mapping.values())
f({3: 'yes', 4: 'no'})
Expand Down

0 comments on commit 6f8480c

Please sign in to comment.