Implicit conversion to boolean in if and while statements

Terry Reedy tjreedy at udel.edu
Sun Jul 15 22:09:44 CEST 2012


On 7/15/2012 12:19 PM, Ian Kelly wrote:
> On Sun, Jul 15, 2012 at 4:56 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> (For the record, I can only think of one trap for the unwary: time
>> objects are false at *exactly* midnight.)
>
> Ugh, that's irritating.  I can't think of any scenario where I would
> ever want the semantics "if timeval (is not midnight):".  This

When printing time tables, midnight may be either 24:00 or 0:00, 
depending on whether it is the end or start of a journey. That could, of 
course, be done by explicit if time == midnight: rather than if not time:.

Whether to change the current behavior was discussed on python-ideas a 
couple of months ago. I believe inertia and back-compatibity and the 
rare use case won.

 > reinforces the point that if you only want to test whether you have
 > None, you should use "is not None" rather than relying on __bool__.

Right.

-- 
Terry Jan Reedy






More information about the Python-list mailing list