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

Steven D'Aprano steve at pearwood.info
Mon May 7 19:31:59 CEST 2012


Alexander Belopolsky wrote:
> On Mon, May 7, 2012 at 11:32 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> Well, less occasional puzzlement is an improvement in itself.
>> Unintuitive behaviour is always a risk for software quality.
> 
> I don't find the current behavior unintuitive.  It is common to
> represent time of day as an integer (number of minutes or seconds
> since midnight) or as a float (fraction of the 24-hour day).  In these
> cases one gets bool(midnight) -> False as an artifact of the
> representation.  

I think you have made a good point there: the behaviour of bool(midnight) is 
an artifact of the internal representation. Unless this behaviour is 
documented, that makes it an implementation detail, and therefore lowers (but 
not eliminates) the barrier to changing it.



[...]
> Note that if we make bool(midnight) -> True, it will not be trivial to
> faithfully reproduce the old behavior.  I want the proponents of the
> change to try it before I explain why it is not easy.

I think it is easy. Instead of either of these:

     if bool(some_time):
         ...
     if some_time:
         ...

write this:

     _MIDNIGHT = datetime.time(0, 0)  # defined once
     if some_time != _MIDNIGHT:
         ...

For code where some_time could be None, write this:

     if not (some_time is None or some_time == _MIDNIGHT):
         ...


Have I missed any common cases?



-- 
Steven




More information about the Python-ideas mailing list