suppressing import errors

David Riley fraveydank at gmail.com
Tue Nov 15 18:15:45 EST 2011


On Nov 15, 2011, at 5:59 PM, Alan Meyer wrote:

> On 11/15/2011 4:20 PM, David Riley wrote:
> ...
>>       None was set to some other value.  The other value might have a type
>>       (such as a container) that could be false in a boolean context!
>> 
>> Obviously, that last bit doesn't apply to modules; they're not going to evaluate as False in general.  I just bristle when I see people writing "if x" when they really mean "if x is not None", perhaps because it's not The Right Way(tm)?  It mostly comes down to aesthetics, I guess.  Write what you really mean.
> 
> Actually Dave, as your quote from PEP 8 says, the difference is real. It's not just aesthetics.

 I guess I meant it's aesthetics when it comes down to determining whether a module is None or not; a module is never going to evaluate to False under any feasible circumstances.  It's not an aesthetic difference when you consider that the global (or local) namespace may be polluted with other variables that have the same name as your module, but then your evaluation would be entirely invalid anyway.

But yes, in the general case, it's much more than an aesthetic difference, which is why I always write "if x is None" when I want to know if it's None (rather than False, 0, [], "", etc).  I have been bitten way too many times by doing the lazy thing to keep doing it.

- Dave




More information about the Python-list mailing list