comparing datetime with date

Steve Holden sholden at
Wed Sep 15 14:43:02 CEST 2004

Andrew Durdin wrote:

> On Tue, 14 Sep 2004 17:16:58 -0400, Tim Peters <tim.peters at> wrote:
>>[Donnal Walter]
>>>I was very surprised to discover that
>>>>>>import datetime
>>>>>>x =, 9, 14)
>>>>>>y = datetime.datetime(2004, 9, 14, 6, 43, 15)
>>>>>>print x == y
>>>How can these two objects be considered equal?
>>They should not be.  Please open a bug report.  The problem is due to that
>>datetime.datetime is a subclass of
> Why should this be considered a bug? In my conception, a
> covers the whole range of times within the date, so that this equality
> makes sense. It also allows for intuitive inequality comparisons
> between datetime.datetime and
Well you do, of course, allow that this appeals to *your* intuition, but 
it seems much more reasonable to me to assume that a date, when compared 
to a datetime, should specify a single canonical time (such as midnight 
at the start of that date).

> To make that more clear, it makes sense to me that comparisons between
> datetime.datetime and should only compare the date part,
> and those between datetime.datetime and datetime.time should only
> compare the time part. This latter however throws an exception; I
> infer from that that the 'pythonic' way is to explicitly make a
> or datetime.time from the datetime.datetime before
> comparing. So I guess I've answered my own question :)

Comparing a datetime and a time makes little sense to me, and I'd prefer 
to be forced to extract the time explicitly from the datetime before 
comparison. A time specifies a point during *any* day, a datetime 
specifies a single point in the time continuum.

When all's said and done I guess this thread mostly highlights the 
unsatisfactory aspects of intuition when used as a basis for agreement 
on complex software specifications :-)


More information about the Python-list mailing list