title: Nested dictionaries in python with error when accessing non-existent key
body: <p>I'm using nested dictionaries as implemented using the AutoVivification class answer at <a href="https://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python">What is the best way to implement nested dictionaries?</a>; namely</p>
  
  <pre><code>class AutoVivification(dict):
      """Implementation of perl's autovivification feature."""
      def __getitem__(self, item):
          try:
              return dict.__getitem__(self, item)
          except KeyError:
              value = self[item] = type(self)()
              return value
  
  a = AutoVivification()
  a['foo']['bar'] = 'spam'
  </code></pre>
  
  <p>thus permitting arbitrary nesting in the dictionary. Is there a way to modify the class such that one could assign values to members using an arbitrary set of keys, but only previously defined sets of keys are allowed when trying to access / read from the member? For example,</p>
  
  <pre><code>print a['foo']['bar']
  print a['foo']['eggs']
  </code></pre>
  
  <p>currently outputs</p>
  
  <pre><code>spam
  {}
  </code></pre>
  
  <p>It would be great if the second one gave an error instead, as a['foo']['eggs'] hasn't been defined...</p>