[Python-ideas] bool(datetime.time(0, 0))
Terry Reedy
tjreedy at udel.edu
Tue May 8 19:57:32 CEST 2012
On 5/8/2012 7:36 AM, M.-A. Lemburg wrote:
> It's perfectly fine for time value to mimic a boolean value
> by following the same paradigm as a float "seconds since midnight"
> value.
Ah, I think this is the key to the dispute as to whether midnight should
be False or True. Is the implementation of time of day as seconds since
midnight essential (then midnight should be False) or accidental (then
midnight should be True like all other times)? Different discussants
disagree on the premise and hence the conclusion.
If one first implements time-of-day as a number representing seconds
from midnight, then bool(midmight) is bool(0) is False, like it or not.
If one later wraps the number as a Time object, as Python did, then
seconds from midnight and the specialness of midnight is essential for
the new object to be a completely back-compatible drop-in replacement
(with augmentations). Anyway, if 'from midnight' is part of the core
concept of the class, the current behavior is correct.
If one starts with time-of-day as a concept independent of linear
numbers, as smoothly flowing around a circle, then making any particular
time of day (or point on the circle) special seems wrong. Indeed, time
of day is the same as local rotation angle with respect to the sum. So
it is as much geometric as numeric.
Abstractly, the second viewpoint seems correct. Pragmatically, however,
civilized humans (those with clocks ;-) have standardized on local
nominal midnight as the base point for numerically measuring time of day.
---
We can also argue the issue both ways from the viewpoint of code
compactness.
False: Let t be a Time instance and midnight be Time(0). Then False
midnight allows 'if t == midnight', which is needed occasionally, to be
abbreviated 'if not t'.
True: Let t be a Time instance or None, such as might be the return from
a function just prior to testing t. Then True midnight allows 'if t is
None', which may be needed on such occasions, to be abbreviated 'if not t'.
While I am comfortable with and love the abbreviations for 0 and empty
(and occasionally None), I would be disinclined, at least at present, to
use either abbreviation for Time conditions. Typing the actual
conditions above was as fast as thinking about getting the abbreviation
to match correctly.
---
If the stdlib had an Elevation class, we could have the same argument
about whether Elevation(0) should be True, like all others, or False.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list