title: How to define a __str__ method for a class?
body: <p>In Python, the <code>object</code> class serves as the root superclass for all the (new-style) classes.  By default at least, applying <code>str</code> and <code>repr</code> to the "class instance" of any subclass of <code>object</code> produces the same result:</p>
  
  <pre><code>&gt;&gt;&gt; class spam(object): pass
  ... 
  &gt;&gt;&gt; str(spam)
  "&lt;class '__main__.spam'&gt;"
  &gt;&gt;&gt; str(spam) == repr(spam)
  </code></pre>
  
  <p>I would like to define a subclass of <code>object</code>, say <code>fancyobject</code>, that is identical to <code>object</code> in every way, except that applying <code>str</code> and <code>repr</code> to <code>fancyobject</code> itself produces different outputs:</p>
  
  <pre><code>&gt;&gt;&gt; class ham(fancyobject): pass
  ...
  &gt;&gt;&gt; str(ham)
  'ham'
  &gt;&gt;&gt; repr(ham)
  "&lt;class '__main__.ham'&gt;"
  </code></pre>
  
  <p>Is there a way to do this in Python?</p>
  
  <p>PS: I'm aware of the <code>__str__</code> special method, but it is my understanding that if class <code>A</code> overrides <code>__str__</code>, then the overriding method is called only when <code>str</code> is called on instances of <code>A</code>, not when it is called on <code>A</code> itself.  I.e.:</p>
  
  <pre><code>&gt;&gt;&gt; class A(object):
  ...     def __str__(self):
  ...         return 'from new __str__: ' + object.__str__(self)
  ... 
  &gt;&gt;&gt; str(A())
  'from new __str__: &lt;__main__.A object at 0x7f79c62a5310&gt;'
  &gt;&gt;&gt; str(A)
  "&lt;class '__main__.A'&gt;"
  </code></pre>