目录

* 一、__format__
<https://www.cnblogs.com/nickchen121/p/10991480.html#一__format__>
一、__format__

* 自定制格式化字符串 date_dic = { 'ymd': '{0.year}:{0.month}:{0.day}', 'dmy':
'{0.day}/{0.month}/{0.year}', 'mdy': '{0.month}-{0.day}-{0.year}', } class
Date: def __init__(self, year, month, day): self.year = year self.month = month
self.day = day def __format__(self, format_spec): #
默认打印ymd的{0.year}:{0.month}:{0.day}格式 if not format_spec or format_spec not in
date_dic: format_spec = 'ymd' fmt = date_dic[format_spec] return
fmt.format(self) d1 = Date(2016, 12, 29) print(format(d1)) 2016:12:29
print('{:mdy}'.format(d1)) 12-29-2016

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信