Nested Mapping

Raymond Hettinger python at
Fri Oct 22 01:19:43 CEST 2010

I'm considering a nested mapping class for the collections module and
would like to solicit feedback from people here on comp.lang.python:

The class is an attempt to generalize from existing use cases (i.e.
the _multimap class in  I also took guidance from the
symbol table analysis tools in ANTLR (see p.173 of Language
Implementation Patterns by Terence Parr).  Nested mappings make it
easy to build chains and trees of scopes (like vocabularies in Forth
or like inheritance hierarchies in Python).

The basic idea is to perform look-ups into a series a dictionaries,
much like Python does with locals, nested scopes, global (module
level) scope and builtins.

Starting with a root context which has a full dictionary API, multiple
child contexts can be created.  New values are stored in the child
contexts, potentially masking those in a parent context (much like
shadowing a Python builltin).  Lookups start with the child node and
work upward through the chain of parents.

There is also a variant where __setitem__ and __delitem__ can mutate
values in parent contexts (much like the nonlocal keyword allows in

The API seeks to fully emulate regular dictionaries.  The only extra
capabilities are:

   d = c.new_child()    # create a subcontext
   e = d.new_child()    # create a subsubcontext
   e.parent                 # find the next enclosing context
   e.root                     # find the outermost enclosing context
   f = c.new_child()     # create a subcontext of c, independent of d
& e

I would appreciate any feedback on the API and on how well it fits
with various use cases that you've found in the wild.


More information about the Python-list mailing list