[Python-ideas] bool(datetime.time(0, 0))

Guido van Rossum guido at python.org
Tue May 8 19:11:39 CEST 2012


I think there's nothing to be done. It's clear that making
datetime.time() be false was a conscious decision when the datetime
module was designed (we thought about *every* aspect of the design
quite a lot -- see (*) below). At the same time knowing what I know
now about common usage I wouldn't design it this way today.

Note that the date and datetime types don't have this problem because
a zero date is invalid; and for the timedelta type having zero be
false is more useful than harmful. But for time, the use case is
marginal and the "trap" is real, even if it is due to poor coding
style. (Good API design should consider avoiding traps for poor coders
one of its goals.)

However, given that it's been a feature for so long I don't think we
can change it. Perhaps we could call it out more in the documentation
(though it's already quite prominent).

(*) I trawled through some history. The original design wiki is only
accessible on the wayback machine:

http://wayback.archive.org/web/20020801000000*/http://zope.org/Members/fdrake/DateTimeWiki/FrontPage

(has many versions between 2002 and 2006). The wiki has no mention of
boolean interpretation for the time type, but the earliest docs for
the C implementation mentions Boolean context:

http://web.archive.org/web/20030119231337/http://www.python.org/dev/doc/devel/lib/datetime-time.html

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list