Issue #16206: Backport dict documentation improvements from 3.2.
Improve the documentation of the dict constructor. This change includes replacing the single-line signature documentation with a more complete multiple-line signature.
This commit is contained in:
parent
d9edd82b7f
commit
def5df6c53
|
@ -275,14 +275,17 @@ available. They are listed here in alphabetical order.
|
||||||
|
|
||||||
|
|
||||||
.. _func-dict:
|
.. _func-dict:
|
||||||
.. function:: dict([arg])
|
.. function:: dict(**kwarg)
|
||||||
|
dict(mapping, **kwarg)
|
||||||
|
dict(iterable, **kwarg)
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
||||||
Create a new data dictionary, optionally with items taken from *arg*.
|
Create a new dictionary. The :class:`dict` object is the dictionary class.
|
||||||
The dictionary type is described in :ref:`typesmapping`.
|
See :class:`dict` and :ref:`typesmapping` for documentation about this
|
||||||
|
class.
|
||||||
|
|
||||||
For other containers see the built in :class:`list`, :class:`set`, and
|
For other containers see the built-in :class:`list`, :class:`set`, and
|
||||||
:class:`tuple` classes, and the :mod:`collections` module.
|
:class:`tuple` classes, as well as the :mod:`collections` module.
|
||||||
|
|
||||||
|
|
||||||
.. function:: dir([object])
|
.. function:: dir([object])
|
||||||
|
|
|
@ -1956,32 +1956,41 @@ Dictionaries can be created by placing a comma-separated list of ``key: value``
|
||||||
pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
|
pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
|
||||||
'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
|
'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
|
||||||
|
|
||||||
.. class:: dict([arg])
|
.. class:: dict(**kwarg)
|
||||||
|
dict(mapping, **kwarg)
|
||||||
|
dict(iterable, **kwarg)
|
||||||
|
|
||||||
Return a new dictionary initialized from an optional positional argument or from
|
Return a new dictionary initialized from an optional positional argument
|
||||||
a set of keyword arguments. If no arguments are given, return a new empty
|
and a possibly empty set of keyword arguments.
|
||||||
dictionary. If the positional argument *arg* is a mapping object, return a
|
|
||||||
dictionary mapping the same keys to the same values as does the mapping object.
|
|
||||||
Otherwise the positional argument must be a sequence, a container that supports
|
|
||||||
iteration, or an iterator object. The elements of the argument must each also
|
|
||||||
be of one of those kinds, and each must in turn contain exactly two objects.
|
|
||||||
The first is used as a key in the new dictionary, and the second as the key's
|
|
||||||
value. If a given key is seen more than once, the last value associated with it
|
|
||||||
is retained in the new dictionary.
|
|
||||||
|
|
||||||
If keyword arguments are given, the keywords themselves with their associated
|
If no positional argument is given, an empty dictionary is created.
|
||||||
values are added as items to the dictionary. If a key is specified both in the
|
If a positional argument is given and it is a mapping object, a dictionary
|
||||||
positional argument and as a keyword argument, the value associated with the
|
is created with the same key-value pairs as the mapping object. Otherwise,
|
||||||
keyword is retained in the dictionary. For example, these all return a
|
the positional argument must be an :term:`iterator` object. Each item in
|
||||||
dictionary equal to ``{"one": 1, "two": 2}``:
|
the iterable must itself be an iterator with exactly two objects. The
|
||||||
|
first object of each item becomes a key in the new dictionary, and the
|
||||||
|
second object the corresponding value. If a key occurs more than once, the
|
||||||
|
last value for that key becomes the corresponding value in the new
|
||||||
|
dictionary.
|
||||||
|
|
||||||
* ``dict(one=1, two=2)``
|
If keyword arguments are given, the keyword arguments and their values are
|
||||||
* ``dict({'one': 1, 'two': 2})``
|
added to the dictionary created from the positional argument. If a key
|
||||||
* ``dict(zip(('one', 'two'), (1, 2)))``
|
being added is already present, the value from the keyword argument
|
||||||
* ``dict([['two', 2], ['one', 1]])``
|
replaces the value from the positional argument.
|
||||||
|
|
||||||
The first example only works for keys that are valid Python
|
To illustrate, the following examples all return a dictionary equal to
|
||||||
identifiers; the others work with any valid keys.
|
``{"one": 1, "two": 2}``::
|
||||||
|
|
||||||
|
>>> a = dict(one=1, two=2)
|
||||||
|
>>> b = dict({'one': 1, 'two': 2})
|
||||||
|
>>> c = dict(zip(('one', 'two'), (1, 2)))
|
||||||
|
>>> d = dict([['two', 2], ['one', 1]])
|
||||||
|
>>> e = {"one": 1, "two": 2}
|
||||||
|
>>> a == b == c == d == e
|
||||||
|
True
|
||||||
|
|
||||||
|
Providing keyword arguments as in the first example only works for keys that
|
||||||
|
are valid Python identifiers. Otherwise, any valid keys can be used.
|
||||||
|
|
||||||
.. versionadded:: 2.2
|
.. versionadded:: 2.2
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue