Python: how can I check whether an object is of type datetime.date?

后端 未结 7 1194
既然无缘
既然无缘 2020-12-24 10:16

I have tried a few obvious options but none of them works:

In [150]: x
Out[150]: datetime.date(2012, 9, 1)

In [151]: type(x)
Out[151]: datetime.date

In [15         


        
7条回答
  •  天命终不由人
    2020-12-24 10:52

    In Python 3.5, isinstance(x, date) works to me:

    >>> from datetime import date
    >>> x = date(2012, 9, 1)
    >>> type(x)
    
    >>> isinstance(x, date)
    True
    >>> type(x) is date
    True
    

提交回复
热议问题