Implicit conversion to boolean in if and while statements

Ethan Furman ethan at stoneleaf.us
Mon Jul 16 14:13:33 EDT 2012


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.  Possibly because it should be 
truthy.  ;)

~Ethan~



More information about the Python-list mailing list