André
André

Reputation: 359

Python overriding class (not instance) special methods

How do I override a class special method?

I want to be able to call the __str__() method of the class without creating an instance. Example:

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())

produces:

<class '__main__.Foo'>
Bar
<class '__main__.StaticFoo'>
StaticBar
<class '__main__.ClassFoo'>
ClassBar

should be:

Bar
Bar
StaticBar
StaticBar
ClassBar
ClassBar

Even if I use the @staticmethod or @classmethod the __str__ is still using the built-in Python definition for __str__. It's only working when it's Foo().__str__() instead of Foo.__str__().

Upvotes: 12

Views: 17359

Answers (3)

bignose
bignose

Reputation: 32309

Why do you want to abuse the meaning of __str__? That method name (like many dunder method names) is special in Python, being an instance method with the meaning "return a string representation of this instance of the class".

If you want a function that just returns a static string, it would be better to have that as a separate function not inside a class.

If you want a constructor that returns a new string, name it something else so it's not clobbering the special __str__ name.

If you want a method for printing a representation of the class, you should not use the name __str__ for that. That name is – as the dunder-style name implies – expected to have particular behaviour as defined in the Python documentation. Choose some (non-dunder) name which you can give your special meaning, and don't forget to make it a class method.

Upvotes: 1

Anurag Uniyal
Anurag Uniyal

Reputation: 88737

Special method __str__ defined in a class works only for the instances of that class, to have the different behavior for class objects you will have to do it in a metaclass of that class e.g. (python 2.5)

class Meta(type):
    def __str__(self):
        return "Klass"

class A(object):
    __metaclass__ = Meta

    def __str__(self):
        return "instance"

print A
print A()

output:

Klass
instance

Upvotes: 20

Charles Merriam
Charles Merriam

Reputation: 20500

I'm not sure what you are trying to do, exactly. Let me just add a bit of random information.

First, add this class:

class FooNew(object):
def __str__(self):
    return 'Fubar'

Print this instead:

if __name__ == '__main__':
    print "You are calling type for an old style class"
    print(Foo)
    print(type.__str__(Foo))
    print(Foo())
    print("But my Python 2.6 didn't match your output for print(Foo)")
    print("You are calling object.str() for a new style class")
    print(FooNew)
    print(object.__str__(FooNew))
    print(FooNew())
    print("Why do you want to change this?")

To get this:

You are calling type for an old style class
__main__.Foo
<class __main__.Foo at 0xb73c9f5c>
Bar
But my Python 2.6 didn't match your output for print(Foo)
You are calling object.str() for a new style class
<class '__main__.FooNew'>
<class '__main__.FooNew'>
Fubar
Why do you want to change this?

Are you absolutely sure you don't want to call a classmethod?

Upvotes: 0

Related Questions