title: Returning int from a __str__ method in Python
body: <p>I know, that the purpose of <code>str()</code> method is to return the string representation of an object, so I wanted to test what happens if I force it to make something else.</p>
  
  <p>I've created a class and an object:</p>
  
  <pre><code>class MyClass(object):
  
      def __str__(self, a=2, b=3):
          return a + b
  
  mc = MyClass()
  </code></pre>
  
  <p>When I call:</p>
  
  <pre><code>print(str(mc))
  </code></pre>
  
  <p>The interpreter complains:</p>
  
  <pre><code>TypeError: __str__ returned non-string (type int)
  </code></pre>
  
  <p>And this is fully understandable because the str() method is trying to return <strong>int</strong>.</p>
  
  <p>But if I try:</p>
  
  <pre><code>print(mc.__str__())
  </code></pre>
  
  <p>I get the output: 5.</p>
  
  <p>So why the interpreter allows me to return int when I call <code>__str__</code> directly, but not when I'm using str(mc) which - as I understand - is also evaluated to <code>mc.__str__()</code>.</p>