Determine the type of an object?

后端 未结 13 2456
余生分开走
余生分开走 2020-11-22 05:50

Is there a simple way to determine if a variable is a list, dictionary, or something else? I am getting an object back that may be either type and I need to be able to tell

13条回答
  •  情书的邮戳
    2020-11-22 06:13

    In general you can extract a string from object with the class name,

    str_class = object.__class__.__name__
    

    and using it for comparison,

    if str_class == 'dict':
        # blablabla..
    elif str_class == 'customclass':
        # blebleble..
    

提交回复
热议问题