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

Skip to content

Commit f341317

Browse files
committed
Issue #16206: Improve the documentation of the dict constructor.
This change includes replacing the single-line signature documentation with a more complete multiple-line signature.
1 parent 7a9953e commit f341317

2 files changed

Lines changed: 39 additions & 28 deletions

File tree

Doc/library/functions.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,17 @@ are always available. They are listed here in alphabetical order.
266266

267267

268268
.. _func-dict:
269-
.. function:: dict([arg])
269+
.. function:: dict(**kwarg)
270+
dict(mapping, **kwarg)
271+
dict(iterable, **kwarg)
270272
:noindex:
271273
272-
Create a new data dictionary, optionally with items taken from *arg*.
273-
The dictionary type is described in :ref:`typesmapping`.
274+
Create a new dictionary. The :class:`dict` object is the dictionary class.
275+
See :class:`dict` and :ref:`typesmapping` for documentation about this
276+
class.
274277

275-
For other containers see the built in :class:`list`, :class:`set`, and
276-
:class:`tuple` classes, and the :mod:`collections` module.
278+
For other containers see the built-in :class:`list`, :class:`set`, and
279+
:class:`tuple` classes, as well as the :mod:`collections` module.
277280

278281

279282
.. function:: dir([object])

Doc/library/stdtypes.rst

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,33 +2114,41 @@ Dictionaries can be created by placing a comma-separated list of ``key: value``
21142114
pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
21152115
'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
21162116

2117-
.. class:: dict([arg])
2118-
2119-
Return a new dictionary initialized from an optional positional argument or
2120-
from a set of keyword arguments. If no arguments are given, return a new
2121-
empty dictionary. If the positional argument *arg* is a mapping object,
2122-
return a dictionary mapping the same keys to the same values as does the
2123-
mapping object. Otherwise the positional argument must be a sequence, a
2124-
container that supports iteration, or an iterator object. The elements of
2125-
the argument must each also be of one of those kinds, and each must in turn
2126-
contain exactly two objects. The first is used as a key in the new
2127-
dictionary, and the second as the key's value. If a given key is seen more
2128-
than once, the last value associated with it is retained in the new
2117+
.. class:: dict(**kwarg)
2118+
dict(mapping, **kwarg)
2119+
dict(iterable, **kwarg)
2120+
2121+
Return a new dictionary initialized from an optional positional argument
2122+
and a possibly empty set of keyword arguments.
2123+
2124+
If no positional argument is given, an empty dictionary is created.
2125+
If a positional argument is given and it is a mapping object, a dictionary
2126+
is created with the same key-value pairs as the mapping object. Otherwise,
2127+
the positional argument must be an :term:`iterator` object. Each item in
2128+
the iterable must itself be an iterator with exactly two objects. The
2129+
first object of each item becomes a key in the new dictionary, and the
2130+
second object the corresponding value. If a key occurs more than once, the
2131+
last value for that key becomes the corresponding value in the new
21292132
dictionary.
21302133

2131-
If keyword arguments are given, the keywords themselves with their associated
2132-
values are added as items to the dictionary. If a key is specified both in
2133-
the positional argument and as a keyword argument, the value associated with
2134-
the keyword is retained in the dictionary. For example, these all return a
2135-
dictionary equal to ``{"one": 1, "two": 2}``:
2134+
If keyword arguments are given, the keyword arguments and their values are
2135+
added to the dictionary created from the positional argument. If a key
2136+
being added is already present, the value from the keyword argument
2137+
replaces the value from the positional argument.
21362138

2137-
* ``dict(one=1, two=2)``
2138-
* ``dict({'one': 1, 'two': 2})``
2139-
* ``dict(zip(('one', 'two'), (1, 2)))``
2140-
* ``dict([['two', 2], ['one', 1]])``
2139+
To illustrate, the following examples all return a dictionary equal to
2140+
``{"one": 1, "two": 2}``::
21412141

2142-
The first example only works for keys that are valid Python identifiers; the
2143-
others work with any valid keys.
2142+
>>> a = dict(one=1, two=2)
2143+
>>> b = dict({'one': 1, 'two': 2})
2144+
>>> c = dict(zip(('one', 'two'), (1, 2)))
2145+
>>> d = dict([['two', 2], ['one', 1]])
2146+
>>> e = {"one": 1, "two": 2}
2147+
>>> a == b == c == d == e
2148+
True
2149+
2150+
Providing keyword arguments as in the first example only works for keys that
2151+
are valid Python identifiers. Otherwise, any valid keys can be used.
21442152

21452153

21462154
These are the operations that dictionaries support (and therefore, custom

0 commit comments

Comments
 (0)