[Python-ideas] Proposal: making __str__ count in time's class

Barry Scott barry at barrys-emacs.org
Wed Mar 8 11:16:43 EST 2017


> On 8 Mar 2017, at 16:01, Francesco Franchina <cescus92 at gmail.com> 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>)

Just use datetime module instead of time?

>>>  datetime.datetime.now().isoformat()
'2017-03-08T16:14:58.448801'

Barry

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170308/9b8281db/attachment-0001.html>


More information about the Python-ideas mailing list