8000 gh-116938: Clarify documentation of `dict` and `dict.update` regardin… · python/cpython@21ac0a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21ac0a7

Browse files
ViicosAlexWaygood
andauthored
gh-116938: Clarify documentation of dict and dict.update regarding the positional argument they accept (#125213)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 89515be commit 21ac0a7

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

Doc/library/stdtypes.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4505,14 +4505,14 @@ can be used interchangeably to index the same dictionary entry.
45054505
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``
45064506

45074507
If no positional argument is given, an empty dictionary is created.
4508-
If a positional argument is given and it is a mapping object, a dictionary
4509-
is created with the same key-value pairs as the mapping object. Otherwise,
4510-
the positional argument must be an :term:`iterable` object. Each item in
4511-
the iterable must itself be an iterable with exactly two objects. The
4512-
first object of each item becomes a key in the new dictionary, and the
4513-
second object the corresponding value. If a key occurs more than once, the
4514-
last value for that key becomes the corresponding value in the new
4515-
dictionary.
4508+
If a positional argument is given and it defines a ``keys()`` method, a
4509+
dictionary is created by calling :meth:`~object.__getitem__` on the argument with
4510+
each returned key from the method. Otherwise, the positional argument must be an
4511+
:term:`iterable` object. Each item in the iterable must itself be an iterable
4512+
with exactly two elements. The first element of each item becomes a key in the
4513+
new dictionary, and the second element the corresponding value. If a key occurs
4514+
more than once, the last value for that key becomes the corresponding value in
4515+
the new dictionary.
45164516

45174517
If keyword arguments are given, the keyword arguments and their values are
45184518
added to the dictionary created from the positional argument. If a key
@@ -4669,10 +4669,11 @@ can be used interchangeably to index the same dictionary entry.
46694669
Update the dictionary with the key/value pairs from *other*, overwriting
46704670
existing keys. Return ``None``.
46714671

4672-
:meth:`update` accepts either another dictionary object or an iterable of
4673-
key/value pairs (as tuples or other iterables of length two). If keyword
4674-
arguments are specified, the dictionary is then updated with those
4675-
key/value pairs: ``d.update(red=1, blue=2)``.
4672+
:meth:`update` accepts either another object with a ``keys()`` method (in
4673+
which case :meth:`~object.__getitem__` is called with every key returned from
4674+
the method). or an iterable of key/value pairs (as tuples or other iterables
4675+
of length two). If keyword arguments are specified, the dictionary is then
4676+
updated with those key/value pairs: ``d.update(red=1, blue=2)``.
46764677

46774678
.. method:: values()
46784679

Lib/_collections_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ def clear(self):
962962

963963
def update(self, other=(), /, **kwds):
964964
''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
965-
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
965+
If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k]
966966
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
967967
In either case, this is followed by: for k, v in F.items(): D[k] = v
968968
'''

0 commit comments

Comments
 (0)
0