Files
python3-cookbook/cookbook/c08/p02_custom_format.py

31 lines
641 B
Python
Raw Permalink Normal View History

2015-01-27 11:44:41 +08:00
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 对象自定义格式化
Desc :
"""
_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
def __format__(self, code):
if code == '':
code = 'ymd'
fmt = _formats[code]
return fmt.format(d=self)
d = Date(2012, 12, 21)
print(d)
print(format(d, 'mdy'))
print('The date is {:ymd}'.format(d))
print('The date is {:mdy}'.format(d))