Unclear datetime.date type when using isinstance

Mailing List lists at fastmail.net
Sat Oct 2 08:12:33 EDT 2010


Was including a input check on a function argument which is expecting a
datetime.date. When running unittest no exception was raised when a
datetime.datetime instance was used as argument. Some playing with the
console lead to this:

>>> import datetime

>>> dt1 = datetime.datetime(2010, 10, 2)
>>> type(dt1)
<type 'datetime.datetime'>
>>> isinstance(dt1, datetime.datetime)
True
>>> isinstance(dt1, datetime.date)
True

>>> dt2 = datetime.date(2010, 10, 2)
>>> type(dt2)
<type 'datetime.date'>
>>> isinstance(dt2, datetime.datetime)
False
>>> isinstance(dt2, datetime.date)
True

My issue (or misunderstanding) is in the first part, while dt1 is a
datetime.date object (confirmed by type), the isinstance(dt1,
datetime.datetime) returns True. Is this correct? If so, is it possible
to verify whether an object is indeed a datetime.date and not a
datetime.datetime object?

For background information; reason my choice was to use the
datetime.date object is that there should not be ay time information in
the object. Of course this can easily be stripped off in the function,
but thought the use of a datetime.date would be a nice shortcut...



More information about the Python-list mailing list