Understanding __str__ and __repr__ in Python's datetime.date
This short tutorial demonstrates how Python's datetime.date object displays differently with __str__ for a readable date string and __repr__ for an unambiguous constructor representation, showing the interpreter’s default use of __repr__ when inspecting objects.
Understanding __str__ and __repr__ in Python's datetime.date
<code>>> import datetime
>>> today = datetime.date.today()
# __str__ result should be readable:
>>> str(today)
'2017-02-02'
# __repr__ result should be explicit:
>>> repr(today)
'datetime.date(2017, 2, 2)'
# Python interpreter uses __repr__ to inspect objects:
>>> today
datetime.date(2017, 2, 2)</code>*Disclaimer: This article is compiled from the web; copyright belongs to the original author. If any source information is incorrect or infringes rights, please contact us for removal or authorization.
Click Read Original to learn more.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.