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

Masklinn masklinn at masklinn.net
Wed Mar 5 11:31:08 CET 2014


On 2014-03-05, at 11:23 , Paul Moore <p.f.moore at gmail.com> wrote:
> On 5 March 2014 10:16, Shai Berger <shai at platonix.com> wrote:
>> http://bugs.python.org/issue13936 is a complaint about the fact that midnight
>> -- datetime.time(0,0,0) -- is evaluated as False in Boolean contexts. It was
>> closed as invalid, under the claim that """It is odd, but really no odder than
>> "zero values" of other types evaluating to false in Boolean contexts""".
>> 
>> I would like to ask for this to be reconsidered; since the ticket was closed,
>> two main arguments were given for this:
> 
> Why on earth would anyone check the boolean value of a time???

Side-effect of e.g. an object with an optional (nullable) time field,
the developer didn't realise a time could be false-ish (because
seriously?) and then it turns out code along the lines of

    if event.start_time:
        # stuff

unexpectedly fails if start_time is midnight.

I've actually done something close recently, didn't expect
more_itertool's peekable iterator could be false-ish[0] because I'd
expect empty sequences to be falsy but not empty iterators.

It turned out to be useful and to simplify my code, but I can see
developers not considering such edge cases unless there's a clear
warning (as in ElementTree which IIRC clearly warns that an element
with no children is false-ish) especially for objects for which
false-ish-ness makes so little sense as a time.

[0] even though the behavior is documented.


More information about the Python-ideas mailing list