title: What does object.__str__ and object.__repr__ do? Warning: It's not the same as Class.__str__ and Class.__repr__
body: <pre><code>class TestClass:
      def __init__(self):
          pass
      def __str__(self):
          return 'You have called __str__'
      def __repr__(self):
          return 'You have called __repr__'
  
  a = TestClass()
  print(object.__repr__(a))
  print(object.__str__(a))
  </code></pre>
  <p><strong>Output:</strong></p>
  <pre><code>&lt;__main__.TestClass object at 0x7fe175c547c0&gt;
  You have called __repr__
  </code></pre>
  <hr />
  <p>What does those two functions do?</p>
  <hr />
  <p>My understanding is that calling <code>str(a)</code> returns <code>a.__str__()</code> and calling <code>repr(a)</code> returns <code>a.__repr__()</code>. <code>print(a)</code> also prints the string, <code>a.__str__()</code> since there is an implicit <code>str(a)</code> conversion going on.</p>
  <p><strong>Note that my question is not a duplicate to another popular question; see my first comment below.</strong></p>
  <p>The behaviour is COUNTERINTUITIVE; doing <code>print(object.__str__(a))</code> prints the repr string instead of the str string.</p>