Skip to content

Commit 3f38ea1

Browse files
miss-islingtonViicosAlexWaygood
authored
[3.12] gh-116938: Clarify documentation of dict and dict.update regarding the positional argument they accept (GH-125213) (#125337)
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 4ab19f9 commit 3f38ea1

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
@@ -4460,14 +4460,14 @@ can be used interchangeably to index the same dictionary entry.
44604460
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``
44614461

44624462
If no positional argument is given, an empty dictionary is created.
4463-
If a positional argument is given and it is a mapping object, a dictionary
4464-
is created with the same key-value pairs as the mapping object. Otherwise,
4465-
the positional argument must be an :term:`iterable` object. Each item in
4466-
the iterable must itself be an iterable with exactly two objects. The
4467-
first object of each item becomes a key in the new dictionary, and the
4468-
second object the corresponding value. If a key occurs more than once, the
4469-
last value for that key becomes the corresponding value in the new
4470-
dictionary.
4463+
If a positional argument is given and it defines a ``keys()`` method, a
4464+
dictionary is created by calling :meth:`~object.__getitem__` on the argument with
4465+
each returned key from the method. Otherwise, the positional argument must be an
4466+
:term:`iterable` object. Each item in the iterable must itself be an iterable
4467+
with exactly two elements. The first element of each item becomes a key in the
4468+
new dictionary, and the second element the corresponding value. If a key occurs
4469+
more than once, the last value for that key becomes the corresponding value in
4470+
the new dictionary.
44714471

44724472
If keyword arguments are given, the keyword arguments and their values are
44734473
added to the dictionary created from the positional argument. If a key
@@ -4624,10 +4624,11 @@ can be used interchangeably to index the same dictionary entry.
46244624
Update the dictionary with the key/value pairs from *other*, overwriting
46254625
existing keys. Return ``None``.
46264626

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

46324633
.. method:: values()
46334634

Lib/_collections_abc.py

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

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

0 commit comments

Comments
 (0)