# When To Use __repr__ vs __str__?
# Emulate what the std lib does:
>>> import datetime
>>> today = datetime.date.today()
# Result of __str__ should be readable:
>>> str(today)
'2017-02-02'
# Result of __repr__ should be unambiguous:
>>> repr(today)
'datetime.date(2017, 2, 2)'
# Python interpreter sessions use
# __repr__ to inspect objects:
>>> today
datetime.date(2017, 2, 2)
版权归属:
admin
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区