Detect if a variable is a datetime object

后端 未结 7 1338
情深已故
情深已故 2021-02-02 05:31

I have a variable and I need to know if it is a datetime object.

So far I have been using the following hack in the function to detect datetime object:

i         


        
相关标签:
7条回答
  • 2021-02-02 06:26

    While using isinstance will do what you want, it is not very 'pythonic', in the sense that it is better to ask forgiveness than permission.

    try:
        do_something_small_with_object() #Part of the code that may raise an 
                                         #exception if its the wrong object
    except StandardError:
        handle_case()
    
    else:
        do_everything_else()
    
    0 讨论(0)
提交回复
热议问题