[Python-ideas] Please reconsider the Boolean evaluation of midnight
Shai Berger
shai at platonix.com
Wed Mar 5 18:56:23 CET 2014
On Wednesday 05 March 2014 19:27:21 Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Wed, Mar 5, 2014 at 11:38 AM, Shai Berger <shai at platonix.com> wrote:
> > So, I rephrase my request: Would anybody object to a silent warning
> > issued whenever a time-object is evaluated as Boolean?
>
> I would. Users should not be penalized for using a documented behavior.
Fair enough. I would also add the warning to the documentation, and call the
behavior "deprecated" (even if no steps will be taken to actually remove it).
> There are legitimate uses for bool(midnight) being False. Midnight is
> special in many contexts. For example, it is uncertain whether midnight
> belongs to the previous or next day. If your application wants to group
> midnight differently from other times - it is perfectly fine to use "if
> dt.time()" instead of a more verbose "if dt.time() != datetime.time(0, 0)".
The question is: Of all the Python lines in existence, where a time is
evaluated as Boolean, what percentage intends to capture midnight, and what
percentage intends to capture an empty field; the latter is a (user) bug, and I
suspect it is the much larger part.
So even though there are legitimate uses for midnight being false, keeping
things as they are is setting a trap for users. It is documented, but highly
unintuitive (as evidenced by many messages on this thread). I think it best if
the gotcha is removed, but if not, I ask at least to have a way to mitigate
the problem.
Thanks,
Shai.
More information about the Python-ideas
mailing list