'if name is not None:' v. 'if name:'

Fredrik Lundh fredrik at pythonware.com
Tue Jul 15 17:11:52 EDT 2008


Victor Noagbodji wrote:

> Well that's exactly why I'm asking. Since None returns False in if
> statements. Why do people use if name is not None: instead of simply
> writing if not name?

Because they want to distinguish between None and other values that 
evaluate to False, of course.  As the page I linked to explains, there 
are many objects that evaluate to false in a boolean context; None is 
just one of them.

Or to spell it out: if you need to distinguish between None and other 
false values, use an explicit test.  If you don't need to distinguish 
between None and other false values, don't use an explicit test.

</F>




More information about the Python-list mailing list