Determine the type of an object?

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

    type() is a better solution than isinstance(), particularly for booleans:

    True and False are just keywords that mean 1 and 0 in python. Thus,

    isinstance(True, int)
    

    and

    isinstance(False, int)
    

    both return True. Both booleans are an instance of an integer. type(), however, is more clever:

    type(True) == int
    

    returns False.

提交回复
热议问题