suppressing import errors
Alan Meyer
ameyer2 at yahoo.com
Tue Nov 15 17:59:12 EST 2011
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.
Consider this:
x = None
if x:
print('if x == true')
else:
print('if x == false')
if x is None:
print('x is None == true')
else:
print('x is none == false')
y = ''
if y:
print('if y == true')
else:
print('if y == false')
if y is None:
print('y is None == true')
else:
print('y is none == false')
The result is:
if x == false
x is None == true
if y == false
y is none == false
Alan
More information about the Python-list
mailing list