datetime gotcha

Frank Millman frank at chagford.com
Wed Dec 11 02:52:05 EST 2019


Hi all

It took me a while to track down a bug that stemmed from this -

 >>> from datetime import date as dt, time as tm, datetime as dtm
 >>> x = dt.today()
 >>> isinstance(x, dt)
True
 >>> isinstance(x, dtm)
False
 >>> y = dtm.now()
 >>> isinstance(y, dt)
True                   <--- ??
 >>> isinstance(y, dtm)
True
 >>> isinstance(y, tm)
False
 >>>

Why is a dtm instance also an instance of dt?

 From the docs, "A datetime object is a single object containing all the 
information from a date object and a time object."

If it was using multiple inheritance, a dtm should also be an instance 
of tm, but it is not.

This is using Python 3.7.2.

Frank Millman



More information about the Python-list mailing list