【Python 技巧】何时使用 __repr__ 与 __str__? PythonCN 2022-03-17 132 阅读1分钟 >>> import datetime >>> today = datetime.date.today() # __str__ 的结果应该是可读的: >>> str(today) '2017-02-02' # __repr__ 的结果应该是明确的: >>> repr(today) 'datetime.date(2017, 2, 2)' # Python 解释器会话使用 __repr__ 来检查对象: >>> today datetime.date(2017, 2, 2)