I've tried
In [16]: import datetime
In [17]: now = datetime.datetime.utcnow()
In [18]: isinstance(now, datetime.date)
Out[18]: True
In [19]: isinstance(now, datetime.datetime)
Out[19]: True
What I expected is that the first isinstance should return False. What happened?
Since datetime
inherits from date
, every instance of datetime
also is an instance of date
, i.e. you can use datetime
instances wherever you would use date
instances. This is a central concept in object-oriented programming.
To modify the check to do what you want, you can test specifically for date
by using type(now) is datetime.date
. Or, you can explicitly exclude datetime
with isinstance(now, datetime.date) and not isinstance(now, datetime.datetime)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With