Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15741 > unrolled thread
| Started by | David Riley <fraveydank@gmail.com> |
|---|---|
| First post | 2011-11-15 16:20 -0500 |
| Last post | 2011-11-15 18:15 -0500 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: suppressing import errors David Riley <fraveydank@gmail.com> - 2011-11-15 16:20 -0500
Re: suppressing import errors Alan Meyer <ameyer2@yahoo.com> - 2011-11-15 17:59 -0500
Re: suppressing import errors David Riley <fraveydank@gmail.com> - 2011-11-15 18:15 -0500
| From | David Riley <fraveydank@gmail.com> |
|---|---|
| Date | 2011-11-15 16:20 -0500 |
| Subject | Re: suppressing import errors |
| Message-ID | <mailman.2750.1321392038.27778.python-list@python.org> |
On Nov 15, 2011, at 3:01 PM, Chris Angelico wrote:
> On Wed, Nov 16, 2011 at 6:39 AM, David Riley <fraveydank@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
[toc] | [next] | [standalone]
| From | Alan Meyer <ameyer2@yahoo.com> |
|---|---|
| Date | 2011-11-15 17:59 -0500 |
| Message-ID | <4EC2EEC0.2090806@yahoo.com> |
| In reply to | #15741 |
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
[toc] | [prev] | [next] | [standalone]
| From | David Riley <fraveydank@gmail.com> |
|---|---|
| Date | 2011-11-15 18:15 -0500 |
| Message-ID | <mailman.2757.1321398951.27778.python-list@python.org> |
| In reply to | #15751 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web