#!/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))