Implement the object.__format__() method instead, and a user can then specify the formatting required with the format() function and str.format() method:
print(format(obj, 'pretty'))
or
print('This object is pretty: {:pretty}'.format(obj))
You probably want to delegate most of the handling of the format on to str.__format__:
def __format__(self, spec):
if spec.endswith('pretty'):
prettified = self.pretty_version()
return prettified.__format__(spec[:-6])
return str(self).__format__(spec)
That way you can still support all the field width and padding alignment options that the default str.__format__ method supports.
Demo:
>>> class Foo():
... def __str__(self):
... return 'plain foo'
... def pretty_version(self):
... return 'pretty foo'
... def __format__(self, spec):
... if spec.endswith('pretty'):
... prettified = self.pretty_version()
... return prettified.__format__(spec[:-6])
... return str(self).__format__(spec)
...
>>> f = Foo()
>>> print(f)
plain foo
>>> print(format(f))
plain foo
>>> print(format(f, 'pretty'))
pretty foo
>>> print(format(f, '>20pretty'))
pretty foo
>>> print('This object is pretty: {:^20pretty}!'.format(f))
This object is pretty: pretty foo !
**kwargsin the constructor rather than the function itself.