[Python-ideas] Please reconsider the Boolean evaluation of midnight

Steven D'Aprano steve at pearwood.info
Wed Mar 5 20:24:19 CET 2014


On Wed, Mar 05, 2014 at 08:44:55PM +0200, Shai Berger wrote:

> > It is unfortunate for your purposes that bool(datetime.time(0, 0, 0)) is
> > False, but it's a documented property of that object, and is highly unlikely
> > to change.
> 
> I think this is what's called 'begging the question' -- this discussion was 
> not about the validity of boolean testing in general, but about the validity 
> of this property of time objects. Yes, it's documented. I'm trying to make it 
> change. There is a lot of support for that, and the opposition seems to be 
> centered on "because it's documented" -- with the two exceptions being an edge 
> use-case (actually checking midnight) and your call to authority below.

There are some very powerful practical reasons for leaving the status 
quo, namely backwards-compatibility and code-churn. These are real 
costs, not for the person who writes the patch, or the people who write 
the tests, and document the change. But these are potential costs for 
the millions of people using Python who may be *relying* on midnight 
being falsey, or nevertheless will have to change their code if this 
change occurs.

There's also the argument from consistency: midnight is the zero point 
of the day, and zero is falsey. It would be surprising if something 
which is essentially zero became truthy.

Compared to those costs, the benefits are insignificant. For library 
authors, they cannot take advantage of this new always-truthy time 
objects for probably eight or ten years, when they drop all support for 
any prior to 3.6 or 3.7. A tiny benefit, discounted even further because 
it is so far in the distance, versus significant costs.

Application authors, only using a single version of Python, may be able 
to make use of this rather sooner: perhaps as little as three years from 
now.


 
> It's a gotcha. Let's remove it. If we don't remove it, let's give ourselves a 
> tool to detect it.

Um, it's easy to detect it.

    not time_obj

returns True if time_obj is midnight and therefore falsey.



-- 
Steven


More information about the Python-ideas mailing list