[Python-ideas] bool(datetime.time(0, 0))

Mike Meyer mwm at mired.org
Tue May 8 17:15:53 CEST 2012


On Tue, 8 May 2012 13:17:22 +0200
Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Tue, 8 May 2012 12:12:05 +0100
> Paul Moore <p.f.moore at gmail.com> wrote:
> > On 8 May 2012 11:53, Antoine Pitrou <solipsis at pitrou.net> wrote:
> > >> If you want to test for None return values, you need to use
> > >> "if is None" or "if is not None".
> > > So who writes the PEP to deprecate __bool__ methods wholesale?
> > I see no need - if you're testing for "true" return values, bool is
> > correct. But if you're testing for None vs an actual value, it's not.
> Well, again, if that's the case, then __bool__ should be deprecated for
> all types where being "true" or "false" doesn't make obvious sense.
> Which is most types, actually.

Not quite, because "practicality beats purity". It should be "all
types where there aren't obviously useful values for 'true' and
'false' in a boolean context."

For container types, "not empty" provides obviously useful etc.  For
numeric types, "nonzero" does that. I think that covers most of the
builtins.

Arguably, the ability to write "if not <container>" instead of "if
empty(<container>)" isn't worth the price of the all-to-common bug of
writing "if not <container>" when you should be writing "if
<container> is None" passing quietly instead of possibly throwing an
exception. But that battle is already lost (and I prefer the current
behavior anyway).

For the case at hand - datetime.time() - the current behavior isn't
obviously useful. If we were doing it from scratch, yeah, maybe it
ought to be true all the time. Or maybe we should follow your
suggestion here, and make converting a datetime.time() to bool throw
an exception. But it doesn't appear to be a problem worth the cost of
fixing.

> Of course, this is a completely vacuous discussion. The reality is that
> a __bool__ exists for all types, we are not deprecating it, and people
> rely on it even though PEP 8 zealots may recommend otherwise.

PEP 8 doesn't recommend against using __bool__. It warns about the
common python coding error of writing "if not x" instead of "if x is
not None" when x may have a value that's both false and not None. This
is a code correctness issue. Checking whether converting something to
bool yields false when you're trying to see if it's some specific
value that happens to convert to false is wrong. It's just as wrong to
write "if not x" rather than "if len(x)" to check to see if x is an
empty container if x might be None as to write "if not x" rather than
"if x is not None" to check to see if x is None. The latter is the far
more common bug in Python programs, which is why PEP 8 warns people
about it instead of the former.

    <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Python-ideas mailing list