自定义字符串输出格式
一、 解决问题
通过 format() 函数和字符串方法使得一个对象能支持自定义的格式化
二、解决方案
重新–format–()钩子函数
三、代码说明
#!/usr/bin/env python
_formats = {
'ymd' : '{d.year}-{d.month}-{d.day}',
'mdy' : '{d.month}/{d.day}/{d.year}',
'dmy' : '{d.day}/{d.month}/{d.year}'
}
class Date:
def __init__(self, year, month,day):
self.year = year
self.month = month
self.day = day
"""
__format__方法给Python的字符串格式化提供了一个钩子
格式化代码的解析工作完全由类本身决定
"""
def __format__(self, code):
if code == '':
code = 'ymd'
fmt = _formats[code]
return fmt.format(d=self)
d = Date(2017, 1, 14)
print(format(d)) #->2017-1-14
print ("The date is {:ymd}".format(d))#->The date is 2017-1-14
print(format(d,'mdy')) #->1/14/2017
from datetime import date
d = date(2017, 1, 14)
print(format(d)) #->2017-01-14
print(format(d, "%A, %B %d, %Y")) #->Saturday, January 14, 2017
四、关联知识
五、总结
字符串格式化传说门,待补充
六、代码地址
github地址:https://github.com/weichen666/python_cookbooka>
目录/文件:eight_selection/learn_class_obj_format.py