title: Python overriding class (not instance) special methods
body: <p>How do I override a class special method?</p>
  
  <p>I want to be able to call the <code>__str__()</code> method of the class without creating an instance. Example:</p>
  
  <pre><code>class Foo:
      def __str__(self):
          return 'Bar'
  
  class StaticFoo:
      @staticmethod
      def __str__():
          return 'StaticBar'
  
  class ClassFoo:
      @classmethod
      def __str__(cls):
          return 'ClassBar'
  
  if __name__ == '__main__':
      print(Foo)
      print(Foo())
      print(StaticFoo)
      print(StaticFoo())
      print(ClassFoo)
      print(ClassFoo())
  </code></pre>
  
  <p>produces:</p>
  
  
  
  <pre class="lang-none prettyprint-override"><code>&lt;class '__main__.Foo'&gt;
  Bar
  &lt;class '__main__.StaticFoo'&gt;
  StaticBar
  &lt;class '__main__.ClassFoo'&gt;
  ClassBar
  </code></pre>
  
  <p>should be:</p>
  
  <pre class="lang-none prettyprint-override"><code>Bar
  Bar
  StaticBar
  StaticBar
  ClassBar
  ClassBar
  </code></pre>
  
  <p>Even if I use the <code>@staticmethod</code> or <code>@classmethod</code> the <code>__str__</code> is still using the built-in Python definition for <code>__str__</code>. It's only working when it's <code>Foo().__str__()</code> instead of <code>Foo.__str__()</code>.</p>