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

Guido van Rossum guido at python.org
Sun Dec 20 17:33:46 EST 2015


On Sun, Dec 20, 2015 at 2:28 PM, Chris Angelico <rosuav at gmail.com> wrote:

> 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()?
>

That fix occurred to me too. However, I didn't propose it, since it's
always a little easy to blame one's own mistakes on the software. Still, I
can't be the only one to ever have been fooled by this, and it is
definitely pretty arcane knowledge what the positional arguments to
timedelta().

I'm just curious on the backward compatibility impact.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20151220/41dade41/attachment.html>


More information about the Python-Dev mailing list