# 自定制format方法
# class Date:
# def __init__(self,year,mon,day):
# self.year=year
# self.mon=mon
# self.day=day
#
# d1=Date(2001,12,30)
#
#
# X="{0.year}:{0.mon}:{0.day}".format(d1)
#
# print(X)
# print(d1.__dict__)
# class Date:
# def __init__(self,year,mon,day):
# self.year=year
# self.mon=mon
# self.day=day
# def __format__(self, format_spec):
# print("执行")
# return "{0.year}:{0.mon}:{0.day}".format(self)
#
# d1=Date(2001,12,30)
# format(d1)
# print(format(d1))
format_dic={
"ymd":"{0.year}{0.mon}{0.day}",
"mdy":"{0.mon}-{0.day}-{0.year}",
"y:m:d":"{0.year}:{0.mon}:{0.day}"
}
class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon=mon
self.day=day
def __format__(self, format_spec):
print("执行")
if not format_spec or format_spec not in format_dic:
format_spec="ymd"
fm=format_dic[format_spec]
return fm.format(self)
d1=Date(2001,12,30)
format(d1)
print(format(d1))
print(format(d1,"y:m:d"))