[Python-ideas] Please reconsider the Boolean evaluation of midnight

Paul Moore p.f.moore at gmail.com
Wed Mar 5 11:44:07 CET 2014


On 5 March 2014 10:31, Masklinn <masklinn at masklinn.net> wrote:
> Side-effect of e.g. an object with an optional (nullable) time field,
> the developer didn't realise a time could be false-ish (because
> seriously?) and then it turns out code along the lines of
>
>     if event.start_time:
>         # stuff
>
> unexpectedly fails if start_time is midnight.

Yeah, I was classing that as "application bug" and it's easy to fix
with an "is not None". Agreed that the odd behaviour of time in a
boolean context is why this whole class of bugs exists, but it's only
a subclass if the wider problem that people shouldn't truth-test
values without thinking - an explicit test is always better (explicit
is better than implicit and all that).

I actually find the idea of truth-testing a value that's expected to
have a type that is always true *more* unintuitive than an explicit
"is not None", so I'd fix the above code regardless of what the truth
value of midnight is.

Paul


More information about the Python-ideas mailing list