Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f8480c

Browse files
MarcoGorelliGuido van Rossum
authored and
Guido van Rossum
committed
Update examples of mutable and non-mutable mapping in cheat sheet (#7620)
1 parent 54e3452 commit 6f8480c

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

docs/source/cheat_sheet_py3.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,13 @@ that are common in idiomatic Python are standardized.
212212
# Mapping describes a dict-like object (with "__getitem__") that we won't
213213
# mutate, and MutableMapping one (with "__setitem__") that we might
214214
def f(my_dict: Mapping[int, str]) -> List[int]:
215+
my_mapping[5] = 'maybe' # if we try this, mypy will throw an error...
215216
return list(my_dict.keys())
216217
217218
f({3: 'yes', 4: 'no'})
218219
219220
def f(my_mapping: MutableMapping[int, str]) -> Set[str]:
220-
my_mapping[5] = 'maybe'
221+
my_mapping[5] = 'maybe' # ...but mypy is OK with this.
221222
return set(my_mapping.values())
222223
223224
f({3: 'yes', 4: 'no'})

0 commit comments

Comments
 (0)