[Python-ideas] Proposal: making __str__ count in time's class
Eric V. Smith
eric at trueblade.com
Sun Apr 9 19:49:22 EDT 2017
On 3/8/2017 11:01 AM, Francesco Franchina wrote:
> Hello everyone,
>
> I'm shortly writing to you about a reflection I lately made upon the
> current functioning of __str__ for the time's class.
>
> Before expressing my thought and proposal, I want to make sure we all
> agree on a simple and clear fact:
> the __str__ magic method is used to give a literal and human-readable
> representation to the object (unlike __repr__).
>
> Generally this is true across the python panorama. It's not true for the
> time class, for example.
> /
> >>> import time
> >>> a = time.localtime()
> >>> a.__str__()
> 'time.struct_time(tm_year=2017, tm_mon=3, tm_mday=8, tm_hour=16,
> tm_min=6, tm_sec=16, tm_wday=2, tm_yday=67, tm_isdst=0)'/
>
> Well, don't get me wrong: the main aim of the __str__ method has been
> accomplished but, imho, not in the most pythonic way.
>
> I just wanted to ask you: what do you think about re-writing the __str__
> of the time class so it would return something like
> ISO 8601 [https://en.wikipedia.org/wiki/ISO_8601
> <https://en.wikipedia.org/wiki/ISO_8601>] format? Wouldn't it be more
> meaningful? Especially in the JS-everywhere-era
> it could be more more productive.
>
>
> *TL;DR*
> __str__ for dates should return a human-readable date format (eg:
> https://en.wikipedia.org/wiki/ISO_8601
> <https://en.wikipedia.org/wiki/ISO_8601>)
>
>
> I'm waiting for your opinions.
> Thank you for your time and ideas!
I don't think we can change __str__ at this point, but I'd support
adding __format__ to make this easier to control. Presumably it would
just call strftime.
Eric.
More information about the Python-ideas
mailing list