Josh Rosenberg <shadowranger+pyt...@gmail.com> added the comment:
Sorry, I think I need examples to grok this in the general case. ChainMap unioned with dict makes sense to me (it's equivalent to update or copy-and-update on the top level dict in the ChainMap). But ChainMap unioned with another ChainMap is less clear. Could you give examples of what the expected end result is for: d1 = {'a': 1, 'b': 2} d2 = {'b': 3, 'c': 4} d3 = {'a': 5, 'd': 6} d4 = {'d': 7, 'e': 8} cm1 = ChainMap(d1, d2) cm2 = ChainMap{d3, d4) followed by either: cm3 = cm1 | cm2 or cm1 |= cm2 ? As in, what is the precise state of the ChainMap cm3 or the mutated cm1, referencing d1, d2, d3 and d4 when they are still incorporated by references in the chain? My impression from what you said is that the plan would be for the updated cm1 to preserve references to d1 and d2 only, with the contents of cm2 (d3 and d4) effectively flattened and applied as an in-place update to d1, with an end result equivalent to having done: cm1 = ChainMap(d1, d2) d1 |= d4 d1 |= d3 (except the key ordering would actually follow d3 first, and d4 second), while cm3 would effectively be equivalent to having done (note ordering): cm3 = ChainMap(d1 | d4 | d3, d2) though again, key ordering would be based on d1, then d3, then d4, not quite matching the union behavior. And a reference to d2 would be preserved in the final result, but not any other original dict. Is that correct? If so, it seems like it's wasting ChainMap's key feature (lazy accumulation of maps), where: cm1 |= cm2 could be equivalent to either: cm1.maps += cm2.maps though that means cm1 wins overlaps, where normal union would have cm2 win, or to hew closer to normal union behavior, make it equivalent to: cm1.map[:0] = cm2.maps prepending all of cm2's maps to have the same duplicate handling rules as regular dicts (right side wins) at the expense of changing which map cm1 uses as the target for writes and deletes. In either case it would hew to the spirit of ChainMap, making dict "union"-ing an essentially free operation, in exchange for increasing the costs of lookups that don't hit the top dict. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue36144> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com