我已经尝试了几个明显的选择,但他们没有工作:Python:如何检查对象是否为datetime.date类型?
In [150]: x
Out[150]: datetime.date(2012, 9, 1)
In [151]: type(x)
Out[151]: datetime.date
In [152]: isinstance(x, datetime.date)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-152-9a298ea6fce5> in <module>()
----> 1 isinstance(x, datetime.date)
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
In [153]: x is datetime.date
Out[153]: False
In [154]: type(x) is datetime.date
Out[154]: False
什么是这样做的正确方法吗?
你可以产生[SSCCE(http://sscce.org)?我应该认为一个四线或五线方案就足够了。我[无法重现您的结果](http://ideone.com/gqdou2)。 – 2013-04-22 16:02:07
您使用的是什么Python版本?对于Python-2.6'isinstance(x,datetime.date)''可以正常工作,因为'type(x)是datetime.date' – RedBaron 2013-04-22 16:03:25
您正在使用哪种版本的Python,以及您是如何生成'x'的? – 2013-04-22 16:03:40