[Python-ideas] Please reconsider the Boolean evaluation of midnight
Shai Berger
shai at platonix.com
Thu Mar 6 23:55:29 CET 2014
On Thursday 06 March 2014 15:21:36 Barry Warsaw wrote:
> On Mar 07, 2014, at 02:40 AM, Stephen J. Turnbull wrote:
> >I also value the psychological benefit of being precise here. I'm not
> >even sure it's possible to fool 'is' into thinking some other object
> >is None, but people do a great job of convincing themselves of things
> >like "datetime.time objects can't be false" all the time. Use the
> >"is None" idiom and you know exactly what you're doing. This latter
> >is close to "just style", of course.
>
> Not just "you" (i.e. the author of the code), but everyone who comes after
> you and reads it. Being more explicit with identity tests against None
> assists future minions in understanding the intent of the code. When using
> the Socratic Method Of Code Reviews, I always ask about whether the author
> really expects the 'var' in "if var:" to be any truth-y value. The answer
> is usually "no", in which case I encourage tightening up the conditional.
>
Wait -- you just argued for replacing
if time_obj:
not by
if time_obj is not None:
-- which is a more relaxed conditional -- but rather by
if isinstance(time_obj, datetime.time):
That seems quite un-pythonic, and *that* - from you - is quite unlikely, so I
probably misunderstood. Do you mind clarifying?
[A guess: The argument you intended to make was about an "if not var:" test
changed to "if var is None:". It does not survive inversion of sense well.]
Thanks,
Shai.
More information about the Python-ideas
mailing list