suppressing import errors
David Riley
fraveydank at gmail.com
Tue Nov 15 16:20:33 EST 2011
More information about the Python-list mailing list
Tue Nov 15 16:20:33 EST 2011
- Previous message (by thread): suppressing import errors
- Next message (by thread): suppressing import errors
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Nov 15, 2011, at 3:01 PM, Chris Angelico wrote: > On Wed, Nov 16, 2011 at 6:39 AM, David Riley <fraveydank at gmail.com> wrote: >> True, and that does avoid polluting namespace. However, you shouldn't be testing for None as a bool; you should instead do an "if <module> is None:" (or, of course, "is not None"). > > Why not? Is there some other way for the module object to evaluate as false? Well, probably not. It was my understanding that "None" evaluating to a Boolean false was not necessarily guaranteed; I've even gotten some warnings from Python to that effect, though I can't recall the context in which that happened. In any case, PEP 8 states: Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators. Also, beware of writing "if x" when you really mean "if x is not None" -- e.g. when testing whether a variable or argument that defaults to 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. - Dave
- Previous message (by thread): suppressing import errors
- Next message (by thread): suppressing import errors
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list