[Python-ideas] Please reconsider the Boolean evaluation of midnight
Skip Montanaro
skip at pobox.com
Wed Mar 5 19:33:08 CET 2014
On Wed, Mar 5, 2014 at 11:24 AM, Shai Berger <shai at platonix.com> wrote:
> If your argument is not that "if obj:" should never be used, except
> when obj actually is Boolean, please elaborate.
My approach continues to be to consider the overall set of objects
from which you draw values to assign to obj. Call that set V. If this
expression is true:
len([bool(x) for x in V]) == 1
then using "if obj:" might be valid (if the one "false" value of that
set is some sort of distinguishing characteristic, as it often
is). If, however, the length of that set is greater than one, you must
be explicit in your conditional expressions. 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. In your case V is None plus the set of all time objects. That
has two values which evaluate to False, so "if obj:" is not
appropriate. If, on the other hand, the set of all values is the set
of all strings or the set of all integers or the set of all lists,
then maybe the falsity of the zero values in those sets ("", 0, [])
has some use.
My last comment on this topic. I will invoke the Uncle Timmy rule
(which I just made up). Never contradict a decision Uncle Timmy
made. He had his reasons. They were very good reasons. They were
almost certainly better than any reasons you can come up with as
counterarguments. Deal with it. :-)
Skip
More information about the Python-ideas
mailing list