comparing datetime with date
Peter Hansen
peter at engcorp.com
Tue Sep 14 08:06:37 EDT 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
> True
>
> How can these two objects be considered equal?
Guessing:
In http://docs.python.org/lib/datetime-datetime.html
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,
to _it_.
> Is there a *general* way
> to test for date != datetime as well as 4.5 != 4.6?
Couldn't say, sorry.
-Peter
More information about the Python-list
mailing list