title: How to subclass int and make it mutable
body: <p>Is it possible to subclass <code>int</code> and make it mutable?</p>
  <p>Consider the following class:</p>
  <pre><code>class CustomBitVector(int):
  
      # bit 7
      @property
      def seventh_property(self):
          return bool(self &amp; (1 &lt;&lt; 7))
  
      @seventh_property.setter
      def seventh_property(self, val):
          self |= bool(val) &lt;&lt; 7
  
      # bit 6
      @property
      def sixth_property(self):
          return bool(self &amp; (1 &lt;&lt; 6))
  
      @sixth_property.setter
      def sixth_property(self, val):
          self |= bool(val) &lt;&lt; 6
  
  
      # ... a few more of these ...
  
      # bit 0
      @property
      def zeroth_property(self):
          return bool(self &amp; (1 &lt;&lt; 0))
  
      @zeroth_property.setter
      def zeroth_property(self, val):
          self |= bool(val) &lt;&lt; 0
  </code></pre>
  <p>I trying to make a nice interface to a bit vector. I'm reading a proprietary protocol off of a socket and I've made classes to represent the messages I'm sending/receiving. Often these messages include bit vectors and it would be nice to deal with them like this.</p>
  <p>This already works great for reading the bit vector values, but setting them doesn't work because <code>int</code> is immutable.</p>
  <p>If I rewrite one of the setters like:</p>
  <pre><code>@sixth_property.setter
  def sixth_property(self, val):
      print 'before:', self
      self |= bool(val) &lt;&lt; 6
      print 'after:', self
  </code></pre>
  <p>Then I get this behavior:</p>
  <pre><code>In [2]: c = CustomBitVector()
  
  In [3]: c.sixth_property
  Out[3]: False
  
  In [4]: c.sixth_property = True
  before: 0
  after: 64
  
  In [5]: c
  Out[5]: 0
  
  In [6]: c.sixth_property
  Out[6]: False
  </code></pre>
  <p>I can see my folly ... I'm assigning to <code>self</code> instead of modifying it. How can I modify <code>self</code> in this case?</p>
  <p>Any crazy hack to make this happen? Maybe using a metaclass or something?</p>
  <hr />
  UPDATE
  <p>I forgot to mention a requirement:</p>
  <p>Instances of <code>CustomBitVector</code> must behave like <code>int</code>. In particular, I need to be able to pass them to <code>struct.pack</code></p>