title: Python pseudo-immutable object field
body: <p>I currently need to partially create a Python object and be able to update it for some time. Although, I must not be able to update it once I used the object as a dictionary key.</p>
  
  <p>Of course there is the solution of marking the fields as private, which is mostly a warning for the programmer, and I will actually go for that solution.</p>
  
  <p>But I stumbled on another solution and I want to know if this could be a good idea, or if it could simply go horribly wrong. Here it is:</p>
  
  <pre><code>class Foo():
      def __init__(self, bar):
          self._bar = bar
          self._has_been_hashed = False
  
      def __hash__(self):
          self._has_been_hashed = True
          return self._bar.__hash__()
  
      def __eq__(self, other):
          return self._bar == other._bar
  
      def __copy__(self):
          return Foo(self._bar)
  
      def set_bar(self, bar):
          if self.has_been_hashed:
              raise FooIsNowImmutable
          else:
              self._bar = bar
  </code></pre>
  
  <p>Some testing proved it to work as desired, I can no longer use set_bar once I, say, used my object as a dictionary key.</p>
  
  <p>What do you think? Is it a good idea? Will it turn against me? Is there an easier way? And is it somehow a bad practice?</p>