Unclear datetime.date type when using isinstance

M

Mailing List

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:
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? If so, is it possible
to verify whether an object is indeed a datetime.date and not a
datetime.datetime object?

For background information; reason my choice was to use the
datetime.date object is that there should not be ay time information in
the object. Of course this can easily be stripped off in the function,
but thought the use of a datetime.date would be a nice shortcut...
 
D

Diez B. Roggisch

Mailing List said:
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:

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? If so, is it possible
to verify whether an object is indeed a datetime.date and not a
datetime.datetime object?

I think you got it wrong here: dt1 is a *datetime* object, and also a
date-object. I guess that's because it conforms to the same protocol -
it has year, month and day and so forth.
For background information; reason my choice was to use the
datetime.date object is that there should not be ay time information in
the object. Of course this can easily be stripped off in the function,
but thought the use of a datetime.date would be a nice shortcut...

Obviously not :) You can of course test not on inheritance (which
isinstance does), but actual type equality:
False


Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top