Implicit conversion to boolean in if and while statements

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 17 08:14:09 CEST 2012


On Mon, 16 Jul 2012 11:13:33 -0700, Ethan Furman wrote:

> Steven D'Aprano wrote:
>> On Sun, 15 Jul 2012 10:19:16 -0600, Ian Kelly wrote:
>>> On Sun, Jul 15, 2012 at 4:56 AM, Steven D'Aprano 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):".
>> 
>> Yes, it is a genuine gotcha. Time values are numbers, and zero is
>> falsey, so midnight is falsey even though it shouldn't be.
>> 
>> There's no good solution here, since we have a conflict between
>> treating time values as time values ("midnight is nothing special") and
>> as numbers ("midnight == 0 which is falsey").
> 
> --> import datetime
> --> mn = datetime.time(0)
> --> mn
> datetime.time(0, 0)
> --> mn == 0
> False
> 
> Apparently, midnight does not equal zero.

My mistake.



-- 
Steven



More information about the Python-list mailing list