what is the Pythonic way to implement a __str__ method with different format options?

拈花ヽ惹草 提交于 2019-12-02 07:30:57

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     !
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!