title: Immutable dictionary in Python 3: how to make keys(), items(), and values() dictionary views immutable
body: <p><strong>Short version: What's the best way to override <code>dict.keys()</code> and friends to keep myself from accidentally modifying my (supposedly) immutable dictionary in Python 3?</strong></p>
  
  <p>In a recent question I asked about <a href="https://stackoverflow.com/questions/10045562/hashing-an-immutable-dictionary-in-python">Hashing an immutable dictionary in Python</a>. Since then I have built an immutable, hashable dictionary I'm happy with. However, I realized it has a hole: the <a href="http://docs.python.org/py3k/library/stdtypes.html#dictionary-view-objects" rel="noreferrer">dictionary views</a> returned by <code>keys()</code>, <code>items()</code>, and <code>values()</code> still allow myself accidentally to mutate my (supposedly) immutable dictionary.</p>
  
  <p>The only question on Stack Overflow I could find about dictionary views was <a href="https://stackoverflow.com/questions/9329537/python-create-own-dict-view-of-subset-of-dictionary">Python create own dict view of subset of dictionary</a>, but that didn't seem to have much to do with my problem, and the answers to <a href="https://stackoverflow.com/questions/2703599/what-would-be-a-frozen-dict">What would a &quot;frozen dict&quot; be?</a> didn't seem to get into overriding <code>keys()</code>, etc.</p>
  
  <p>Would doing something like this prevent me from accidentally modifying, for example, the keys of my immutable dictionary?</p>
  
  <pre class="lang-python prettyprint-override"><code>class FrozenCounter(collections.Counter):
      "Model an hashable multiset as an immutable dictionary."
      # ...
      def keys(self):
          return list(super().keys())
      def values(self):
          return list(super().values())
      def items(self):
          return list(super().items())
  </code></pre>
  
  <p><hr /></p>
  
  <h2>What I've gathered from the answers</h2>
  
  <p>I can't read, mainly. </p>
  
  <p>dictviews cannot modify dicts. In the <a href="http://docs.python.org/py3k/library/stdtypes.html#dictionary-view-objects" rel="noreferrer">Python 3 documentation</a>, I misread, "They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes" as saying "when the view changes, the dictionary reflects these changes." Obviously that is not what the documentation said.</p>