[Python-Dev] Change the repr for datetime.timedelta (was Re: Asynchronous context manager in a typical network server)

Chris Angelico rosuav at gmail.com
Sun Dec 20 17:28:21 EST 2015


On Mon, Dec 21, 2015 at 9:02 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Fri, Dec 18, 2015 at 4:09 PM, Guido van Rossum <guido at python.org> wrote:
>>>
>>>
>>> It's 11 days. Which is pretty reasonable server uptime.
>>
>>
>> Oops, blame the repr() of datetime.timedelta. I'm sorry I so rashly
>> thought I could do better than the OP.
>
>
> A helpful trivia: a year is approximately π times 10 million seconds.

Sadly doesn't help here, as the timedelta for a number of years looks like this:

>>> datetime.timedelta(days=365*11)
datetime.timedelta(4015)

Would there be value in changing the repr to use keyword arguments?
Positional arguments might well not correspond to the way they were
created, and unless you happen to know what the fields mean, they're a
little obscure:

>>> datetime.timedelta(weeks=52,minutes=1488)
datetime.timedelta(365, 2880)

Worse, help(datetime.timedelta) in 3.6 doesn't document the
constructor at all. There's no mention of __init__ at all, __new__ has
this useless information:

 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.

and aside from there being three data descriptors, there's nothing to
suggest that you construct these things with timedelta(days, seconds,
microseconds). Definitely no indication that you can use other keyword
args.

Is this something worth fixing, or is it acceptable to drive people to
fuller documentation than help()?

ChrisA


More information about the Python-Dev mailing list