File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,21 @@ The class can be used to simulate nested scopes and is useful in templating.
100
100
:func: `super ` function. A reference to ``d.parents `` is equivalent to:
101
101
``ChainMap(*d.maps[1:]) ``.
102
102
103
+ Note, the iteration order of a :class: `ChainMap() ` is determined by
104
+ scanning the mappings last to first::
105
+
106
+ >>> baseline = {'music': 'bach', 'art': 'rembrandt'}
107
+ >>> adjustments = {'art': 'van gogh', 'opera': 'carmen'}
108
+ >>> list(ChainMap(adjustments, baseline))
109
+ ['music', 'art', 'opera']
110
+
111
+ This gives the same ordering as a series of :meth: `dict.update ` calls
112
+ starting with the last mapping::
113
+
114
+ >>> combined = baseline.copy()
115
+ >>> combined.update(adjustments)
116
+ >>> list(combined)
117
+ ['music', 'art', 'opera']
103
118
104
119
.. seealso ::
105
120
Original file line number Diff line number Diff line change @@ -114,6 +114,20 @@ def test_basics(self):
114
114
self .assertEqual (f ['b' ], 5 ) # find first in chain
115
115
self .assertEqual (f .parents ['b' ], 2 ) # look beyond maps[0]
116
116
117
+ def test_ordering (self ):
118
+ # Combined order matches a series of dict updates from last to first.
119
+ # This test relies on the ordering of the underlying dicts.
120
+
121
+ baseline = {'music' : 'bach' , 'art' : 'rembrandt' }
122
+ adjustments = {'art' : 'van gogh' , 'opera' : 'carmen' }
123
+
124
+ cm = ChainMap (adjustments , baseline )
125
+
126
+ combined = baseline .copy ()
127
+ combined .update (adjustments )
128
+
129
+ self .assertEqual (list (combined .items ()), list (cm .items ()))
130
+
117
131
def test_constructor (self ):
118
132
self .assertEqual (ChainMap ().maps , [{}]) # no-args --> one new dict
119
133
self .assertEqual (ChainMap ({1 :2 }).maps , [{1 :2 }]) # 1 arg --> list
You can’t perform that action at this time.
0 commit comments