Proposal: making __str__ count in time's class

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] 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) I'm waiting for your opinions. Thank you for your time and ideas! Francesco Franchina

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

On 03/08/2017 08:01 AM, Francesco Franchina wrote:
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__).
If __str__ has not been defined, then __repr__ is used instead.
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)
Which is what that looks like. -- ~Ethan~

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.
participants (4)
-
Barry Scott
-
Eric V. Smith
-
Ethan Furman
-
Francesco Franchina