I also prefer the explicit if a is None, since it signals my intent better. I honestly try to only use if a: when a is a boolean, since I don't want to have to remember how __bool__ is implemented for every type out there. In my opinion, explicit is better than implicit for being able to read your own code in a month's time.
For example, if I have a variable which is initialized as None and later want to find out if it was set, I might want to check and see if it was still None. If it's not, I want to do something.
if a is not None:
do_something()
If I rewrite that as:
if not a:
do_something()
do_something will also be called if a was initialized, but to a blank string, which was not the intention. Lots of types have some concept of a "false" value. As /u/steve_no points out in another comment: 0, '', [], {}, midnight all evaluate to False.
12
u/steve_no Jun 23 '14
The normal code "is" example seems strange. I think the non-pythonic thing most programmers do with respect to "is" is not using it, for example:
when they should be doing: