title: immutable objects in Python that can have weak references
body: <p>I've been subclassing <code>tuple</code> or using <code>namedtuple</code> blissfully for a few years, but now I have a use case where I need a class that can be used as a weak referent. And today I learned <a href="https://mail.python.org/pipermail/python-list/2005-March/346300.html" rel="noreferrer"><code>tuple</code>s don't support weak references</a>.  </p>
  
  <p>Is there another way to create an immutable object in Python with a fixed set of attributes? I don't need the numeric indexing or variable width of a tuple. </p>
  
  <pre><code>class SimpleThingWithMethods(object):
      def __init__(self, n, x):
          # I just need to store n and x as read-only attributes 
      ... ??? ...
  </code></pre>
  
  <hr>
  
  <p>I guess this raises the obvious question of why immutable; "Pythonic" code usually just assumes we're all adults here and no one in their right mind would reach into a class and muck with its values if it risks ruining the class invariants. In my case I have a class in a library and I am worried about accidental modification of objects by end-users. The people I work with sometimes make incorrect assumptions about my code and start doing things I did not expect, so it's much cleaner if I can raise an error if they accidentally modify my code.</p>
  
  <p>I'm not so worried about bulletproof immutability; if someone really nefarious wants to go and modify things, ok, fine, they're on their own. I just want to make it hard to accidentally modify my objects.</p>