While skimming through the release notes for the upcoming Python 3.5 version I noticed a reference to a bug in the datetime module. Since I use that basically whenever I do anything in Python I got curious.
Turns out that up until 3.5 there was a datetime.time
instance that got
evaluated to False in a boolean context (issue13936):
> bool(datetime.time(0, 0, 0))
False
So basically whenever you have a time instance, make sure to compare it against another one of None, but never do the implicit boolean check!
While this behaviour has been documented …
in Boolean contexts, a time object is considered to be true if and only if, after converting it to minutes and subtracting utcoffset() (or 0 if thatβs None), the result is non-zero.
… that doesn’t change that it is highly un-intuitive. The reasons for this implementation have been laid out in the issue but I’m really glad that with 3.5 all time instance will evaluate to true!
Do you want to give me feedback about this article in private? Please send it to comments@zerokspot.com.
Alternatively, this website also supports Webmentions. If you write a post on a blog that supports this technique, I should get notified about your link π