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

Barry Warsaw barry at python.org
Thu Mar 6 21:21:36 CET 2014


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.

There are valid cases where a test is really trying to catch any truth-y or
false-y value.  I usually encourage a comment to clarify the author's intent
in those cases.

-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140306/e5450795/attachment-0001.sig>


More information about the Python-ideas mailing list