python之format

format_dict={
'ymd':'{0.year} {0.month} {0.day}',
'y-m-d':'{0.year}-{0.month}-{0.day}',
'm:d:y':'{0.month}:{0.day}:{0.year}'
}
class time:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
def __format__(self,item):
if not item or item not in format_dict:
item='ymd'
return format_dict[item].format(self)

f=time(2020,2,23)
print(format(f,'y:m:t'))

猜你喜欢

转载自www.cnblogs.com/cxydnxs/p/12346121.html