title: How does len work?
body: <p>How does len work on Python?</p>
  
  <p>Look at this example:</p>
  
  <pre><code>class INT(int):
      pass
  
  class STR(str):
  
      def __len__(self):
          return INT(42)
  
  q = STR('how').__len__()
  print q, type(q)
  q = len(STR('how'))
  print q, type(q)
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>42 &lt;class '__main__.INT'&gt;
  42 &lt;type 'int'&gt;
  </code></pre>
  
  <p>How can I handle it so len returns an INT instance?</p>
  
  <p><strong>Answers suggest that the only solution is overriding len</strong></p>
  
  <p>This is my alternative implementation. It doesn't seem very harmful. </p>
  
  <pre><code>original_len = len
  def len(o):
      l = o.__len__()
      if isinstance(l, int):
          return l
      original_len(o)
  </code></pre>