Determine the type of an object?

后端 未结 13 2362
余生分开走
余生分开走 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:06

    For the sake of completeness, isinstance will not work for type checking of a subtype that is not an instance. While that makes perfect sense, none of the answers (including the accepted one) covers it. Use issubclass for that.

    >>> class a(list):
    ...   pass
    ... 
    >>> isinstance(a, list)
    False
    >>> issubclass(a, list)
    True
    

提交回复
热议问题