comparing datetime with date

Peter Hansen peter at
Tue Sep 14 14:06:37 CEST 2004

Donnal Walter wrote:

> I was very surprised to discover that
>  >>> import datetime
>  >>> x =, 9, 14)
>  >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)
>  >>> print x == y
> True
> How can these two objects be considered equal? 


it says this """Note: In order to stop comparison from falling back to
the default scheme of comparing object addresses, datetime comparison
normally raises TypeError if the other comparand isn't also a datetime
object. However, NotImplemented is returned instead if the other
comparand has a timetuple attribute. This hook gives other kinds of date
objects a chance at implementing mixed-type comparison. """
(the docs for the date class say the same).

I suspect this means simply that *does* use this
behaviour to provide a mixed-type comparison that makes sense,
to _it_.

 > Is there a *general* way
 > to test for date != datetime as well as 4.5 != 4.6?

Couldn't say, sorry.


More information about the Python-list mailing list