Extending string formatting with custom conversion types

不羁的心 提交于 2019-12-11 09:26:04

问题


string.Formatter allows the extension of new style formatting with custom conversion types.

Is this possible for the older "%"-style formatting strings too? Is there a library for this?


回答1:


Maybe

class CustomFormat(object):
    def __init__(self, obj):
        self.Object = obj

    def __str__(self):
        return str(self.Object).upper()  # your magic goes here...

print "abc%s" % customFormat("hello")
# abcHELLO


来源:https://stackoverflow.com/questions/18394385/extending-string-formatting-with-custom-conversion-types

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