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

Bruce Leban bruce at leapyear.org
Wed Mar 5 22:04:56 CET 2014


On Wed, Mar 5, 2014 at 11:24 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> 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.
>

I don't think it's consistent now.

In Boolean contexts ...

   - a timedelta<http://docs.python.org/3.4/library/datetime.html#datetime.timedelta>
object
   is considered to be true if and only if it isn't equal to timedelta(0).
   - all date<http://docs.python.org/3.4/library/datetime.html#datetime.date>
objects
   are considered to be true.
   - all datetime<http://docs.python.org/3.4/library/datetime.html#datetime.datetime>
objects
   are considered to be true.
   - a time <http://docs.python.org/3.4/library/datetime.html#datetime.time>
object
   is considered to be true if and only if, after converting it to minutes and
   subtracting utcoffset() (or 0 if that's None), the result is non-zero.

And the documentation is not exactly clear for understanding when a time
object is falsey. In particular, the "converting it to minutes" part seems
irrelevant as a time of 1 microsecond from midnight is truthy and
timedeltas are not numbers and are not in minutes. Perhaps a reasonable
improvement would be to change this to:

   - a time <http://docs.python.org/3.4/library/datetime.html#datetime.time>
object
   is considered to be true unless it represents exactly midnight local time,
   that is, it's false if subtracting the utcoffset() from the time
   produces an all-zero result (or it's all-zero to start with if the
   utcoffset is None).


--- Bruce
Learn how hackers think: http://j.mp/gruyere-security
https://www.linkedin.com/in/bruceleban
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140305/7412a561/attachment.html>


More information about the Python-ideas mailing list