diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-02-21 17:12:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-21 17:12:15 (GMT) |
commit | 86f093f71a594dcaf21b67ba13dda72863e9bde9 (patch) | |
tree | 8e6334a7628080757c21ea0d9aa855e16bf22836 /Doc | |
parent | 7463884f6993edcd92bec56213ee1959034fd31c (diff) | |
download | cpython-86f093f71a594dcaf21b67ba13dda72863e9bde9.zip cpython-86f093f71a594dcaf21b67ba13dda72863e9bde9.tar.gz cpython-86f093f71a594dcaf21b67ba13dda72863e9bde9.tar.bz2 |
bpo-36060: Document how collections.ChainMap() determines iteration order (GH-11969)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/collections.rst | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index d847d6b..ca2f116 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -100,6 +100,21 @@ The class can be used to simulate nested scopes and is useful in templating. :func:`super` function. A reference to ``d.parents`` is equivalent to: ``ChainMap(*d.maps[1:])``. + Note, the iteration order of a :class:`ChainMap()` is determined by + scanning the mappings last to first:: + + >>> baseline = {'music': 'bach', 'art': 'rembrandt'} + >>> adjustments = {'art': 'van gogh', 'opera': 'carmen'} + >>> list(ChainMap(adjustments, baseline)) + ['music', 'art', 'opera'] + + This gives the same ordering as a series of :meth:`dict.update` calls + starting with the last mapping:: + + >>> combined = baseline.copy() + >>> combined.update(adjustments) + >>> list(combined) + ['music', 'art', 'opera'] .. seealso:: |