title: Extending base classes in Python
body: <p>I'm trying to extend some "base" classes in Python:</p>
  
  <pre><code>class xlist (list):
      def len(self):
          return len(self)
  
      def add(self, *args):
          self.extend(args)
          return None
  
  
  class xint (int):
      def add(self, value):
          self += value
          return self
  
  
  x = xlist([1,2,3])
  print x.len()   ## &gt;&gt;&gt; 3 ok
  print x         ## &gt;&gt;&gt; [1,2,3] ok
  x.add (4, 5, 6)
  print x         ## &gt;&gt;&gt; [1,2,3,4,5,6] ok
  
  x = xint(10)
  print x         ## &gt;&gt;&gt; 10 ok
  x.add (2)
  print x         ## &gt;&gt;&gt; 10  # Not ok (#1)
  
  print type(x)         ## &gt;&gt;&gt; &lt;class '__main__.xint'&gt; ok
  x += 5
  print type(x)         ## &gt;&gt;&gt; &lt;type 'int'&gt;  # Not ok (#2)
  </code></pre>
  
  <p>It works fine in the <em>list</em> case because the <em>append</em> method modifies the object "in place", without returning it. But in the <em>int</em> case, the <em>add</em> method doesn't modify the value of the external <em>x</em> variable. I suppose that's fine in the sense that <em>self</em> is a local variable in the <em>add</em> method of the class, but this is preventing me from modifying the initial value assigned to the instance of the class.</p>
  
  <p>Is it possible to extend a class this way or should I define a class property with the base type and map all the needed methods to this property?</p>