当前位置: 首页 > 后端技术 > Python

【Python小技巧】什么时候使用__repr__和__str__?

时间:2023-03-25 21:24:59 Python

>>>importdatetime>>>today=datetime.date.today()#__str__的结果应该是可读的:>>>str(today)'2017-02-02'#__repr__的结果应该是显式:>>>repr(today)'datetime.date(2017,2,2)'#Python解释器会话使用__repr__检查对象:>>>todaydatetime.date(2017,2,2)