[Python-ideas] bool(datetime.time(0, 0))
Alexander Belopolsky
alexander.belopolsky at gmail.com
Mon May 7 19:44:29 CEST 2012
On Mon, May 7, 2012 at 1:31 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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?
Yes, your code will raise an exception if some_time has tzinfo set.
This is exactly the issue that I expected you to miss, so I rest my
case. :-)
More information about the Python-ideas
mailing list