title: Exposing `defaultdict` as a regular `dict`
body: <p>I am using <code>defaultdict(set)</code> to populate an internal mapping in a very large data structure. After it's populated, the whole structure (including the mapping) is exposed to the client code. At that point, I don't want anyone modifying the mapping. </p>
  
  <p>And nobody does, intentionally. But sometimes, client code may by accident refer to an element that doesn't exist. At that point, a normal dictionary would have raised <code>KeyError</code>, but since the mapping is <code>defaultdict</code>, it simply creates a new element (an empty set) at that key. This is quite hard to catch, since everything happens silently. But I need to ensure this doesn't happen (the semantics actually doesn't break, but the mapping grows to a huge size).</p>
  
  <p>What should I do? I can see these choices:</p>
  
  <ol>
  <li><p>Find all the instances in current and future client code where a dictionary lookup is performed on the mapping, and convert it to <code>mapping.get(k, {})</code> instead. This is just terrible.</p></li>
  <li><p>"Freeze" <code>defaultdict</code> after the data structure is fully initialized, by converting it to <code>dict</code>. (I know it's not really frozen, but I trust client code to not actually write <code>mapping[k] = v</code>.) Inelegant, and a large performance hit.</p></li>
  <li><p>Wrap <code>defaultdict</code> into a <code>dict</code> interface. What's an elegant way to do that? I'm afraid the performance hit may be huge though (this lookup is heavily used in tight loops).</p></li>
  <li><p>Subclass <code>defaultdict</code> and add a method that "shuts down" all the <code>defaultdict</code> features, leaving it to behave as if it's a regular <code>dict</code>. It's a variant of 3 above, but I'm not sure if it's any faster. And I don't know if it's doable without relying on the implementation details.</p></li>
  <li><p>Use regular <code>dict</code> in the data structure, rewriting all the code there to first check if the element is in the dictionary and adding it if it's not. Not good.</p></li>
  </ol>