__format__
16lz
2021-04-15
目录
- 一、__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©著作权归作者所有:来自51CTO博客作者wx5b1fd43180419的原创作品,如需转载,请注明出处,否则将追究法律责任
更多相关文章
- 格式化输出的三种方式
- 字符串类型内置方法
- 【DB笔试面试494】Oracle中哪个包用于生成随机值?
- C语言编程笔试题(一)
- C语言进阶(五)——字符串+内存函数的介绍
- 【DB笔试面试244】在Oracle中,什么是Quote(q)语法?
- C语言之库函数的模拟与使用
- 调用CXF框架的webservie restful风格服务报415错误的解决方法
- Linux格式化报错mkfs.ext4: undefined symbol: ext2fs_numeric_p