YADTR (Yet Another DateTime Rant)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Mar 28 00:41:59 CET 2014

On Thu, 27 Mar 2014 08:52:24 -0400, Roy Smith wrote:

> In article <mailman.8613.1395917059.18130.python-list at python.org>,
>  Chris Angelico <rosuav at gmail.com> wrote:
>> It's not "equally braindead", it follows a simple and logical rule:
>> Only the day portion is negative.
> Simple and logical, yes.  But also entirely braindead.

Do you think it is "braindead" for __str__ to return something which 
follows the internal representation of the object?

Note that I'm not asking if that is the best choice for every object, or 
even the best choice for any object. You've made an extremely strong 
claim, by calling something "braindead" you're essentially arguing that 
this is an *utterly irredeemable choice whatsoever* with absolutely no 
redeeming features.

Hyperbole is fun, that's why we do it. It allows us to build up a nice 
feeling of righteous indignation over something utterly outrageous and 
indefensible, without having to worry about inconvenient distractions 
like other points of view :-)

>> That might not be perfectly suited to all situations
> Give ma a real-life situation where you would want such behavior.

Easy -- I'm debugging timedelta routines, and I want to easily see that 
the timedeltas calculated match what I expect them to be when I print 
them. The quickest, easiest and simplest way is for str(timedelta) to 
follow the internal representation.

Oh look, that's exactly what the docs say:

"String representations of timedelta objects are normalized similarly to 
their internal representation. This leads to somewhat unusual results for 
negative timedeltas."

as Skip has already pointed out.

Is this the *best* choice for a timedelta? Probably not. But it's a 
simple, logical, *reasonable* choice. It only fails the "reasonable" test 
if you insist that timedelta objects *must* match the time-keeping 
conventions of native English speakers, and any other convention is 
unspeakable. For all we know, maybe (say) Koreans or Egyptians or Gaelic 
Scots do say something like "It was a day ago, less four hours" meaning 
twenty hours ago. Stranger things happen in natural language, and we 
ought to be cautious about insisting that English conventions are 

(Given Johannes Bauer's reaction, we can be fairly certain that German 
speakers follow a similar convention to English speakers. Which shouldn't 
be terribly surprising, since English is a Germanic language.)

A few seconds searching the bug tracker shows that this is not, in fact, 
a bug in timedelta but probably a deliberate feature:


This one is also relevant: http://bugs.python.org/issue1569623

Alas, the discussion appears to have been on #python-dev, which 
(probably) means it is not archived anywhere.

Steven D'Aprano

More information about the Python-list mailing list