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

Nick Coghlan ncoghlan at gmail.com
Tue May 8 09:02:04 CEST 2012


On Tue, May 8, 2012 at 3:42 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, May 08, 2012 at 01:57:13PM +1000, Nick Coghlan wrote:
>> On Tue, May 8, 2012 at 12:57 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> > The behavior is broken.  Midnight is not False.
>>
>> Whereas I disagree - I see having zero hour be false as perfectly
>> reasonable behaviour (not necessarily *useful*, but then having all
>> time objects report as True in boolean context isn't particularly
>> useful either).
>
> On the contrary, it can be very useful to have all objects of some
> classes treated as true. For example, we can write:
>
> mo = re.search(a, b)
> if mo:
>    do_something_with(mo)
>
> without having to worry about the case where a valid MatchObject happens
> to be false.
>
> Consider:
>
> t = get_job_start_time()  # returns a datetime.time object, or None
> if t:
>    do_something_with(t)
>
>
> Oops, we have a bug. If the job happens to have started at exactly
> midnight, it will wrongly be treated as false.

IMO, you've completely misdiagnosed the source of that bug. Never
*ever* rely on boolean evaluation when testing against None. *Always*
use the "is not None" trailer.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list