On Sat, Oct 2, 2010 at 5:12 AM, Mailing List <li...@fastmail.net> wrote: > Was including a input check on a function argument which is expecting a > datetime.date. When running unittest no exception was raised when a > datetime.datetime instance was used as argument. Some playing with the > console lead to this: > >>>> import datetime > >>>> dt1 = datetime.datetime(2010, 10, 2) >>>> type(dt1) > <type 'datetime.datetime'> >>>> isinstance(dt1, datetime.datetime) > True >>>> isinstance(dt1, datetime.date) > True > >>>> dt2 = datetime.date(2010, 10, 2) >>>> type(dt2) > <type 'datetime.date'> >>>> isinstance(dt2, datetime.datetime) > False >>>> isinstance(dt2, datetime.date) > True > > My issue (or misunderstanding) is in the first part, while dt1 is a > datetime.date object (confirmed by type), the isinstance(dt1, > datetime.datetime) returns True. Is this correct?
I think you accidentally swapped "date" and "datetime" in this paragraph (otherwise, it's rather nonsensical). Anyway, to answer what I think you were trying to ask: >>> from datetime import date, datetime >>> issubclass(datetime, date) True >>> datetime.__bases__ (<type 'datetime.date'>,) Recall how isinstance() works when sub/superclasses are involved: >>> class A(object): ... pass ... >>> isinstance(object(), object) True >>> isinstance(A(), object) True > If so, is it possible > to verify whether an object is indeed a datetime.date and not a > datetime.datetime object? Of course: >>> this_moment = datetime.now() >>> today = date.today() >>> type(today) is date True >>> type(this_moment) is date False Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list