comparing datetime with date
peter at engcorp.com
Tue Sep 14 14:06:37 CEST 2004
Donnal Walter wrote:
> I was very surprised to discover that
> >>> import datetime
> >>> x = datetime.date(2004, 9, 14)
> >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)
> >>> print x == y
> 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 datetime.date *does* use this
behaviour to provide a mixed-type comparison that makes sense,
> 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