[Python-Dev] Change the repr for datetime.timedelta (was Re: Asynchronous context manager in a typical network server)
Random832
random832 at fastmail.com
Mon Dec 21 10:39:09 EST 2015
Guido van Rossum <guido at python.org> writes:
> I'm sure that one often catches people by surprise. However, I don't
> think we can fix that one without also fixing the values of the
> attributes -- in that example days is -1 and seconds is 86340 (which
> will *also* catch people by surprise). And changing that would be
> much, much harder for backwards compatibility reasons-- we'd have to
> set days to 0 and seconds to -60, and suddenly we have a much murkier
> invariant, instead of the crisp
>
> 0 <= microseconds < 1000000
> 0 <= seconds < 60
I don't really see it as murky:
0 <= abs(microseconds) < 1000000
0 <= abs(seconds) < 60
(days <= 0) == (seconds <= 0) == (microseconds <= 0)
(days >= 0) == (seconds >= 0) == (microseconds >= 0)
The latter are more easily phrased in english as "all nonzero
attributes have the same sign". I think the current behavior is
rather as if -1.1 were represented as "-2+.9". The attributes
probably can't be fixed without breaking backwards
compatibility, though. How about "-timedelta(0, 60)"?
More information about the Python-Dev
mailing list