comparing datetime with date

Diez B. Roggisch deetsNOSPAM at web.de
Tue Sep 14 15:20:54 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
> True

I think thats perfectly legal - a date object should care only about if the
object it is compared to contains some sort of valid date information, and
restrict its comparision to that.

The matter is that you try to compare two different things here - so its up
to the implementation if how it deals with this, as there is no canonical
way to compare two things that aren't even structural equivalent.

The current implementation chose one way to do it - and thats as good as any
other one could think of, and certainly has some convenience advantage on
its side.
 
> How can these two objects be considered equal? Is there a *general* way
> to test for date != datetime as well as 4.5 != 4.6?

Try making a datetime-object from your date, with zeros for the time part -
then you'll get what you want.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list