How to check if variable is a specific class in python?

前端 未结 3 1539
小鲜肉
小鲜肉 2020-12-31 00:43

I have a variable \"myvar\" that when I print out its type(myvar)

the output is:


If

3条回答
  •  太阳男子
    2020-12-31 01:19

    Use isinstance, this will return true even if it is an instance of the subclass:

    if isinstance(x, my.object.kind)
    

    Or:

    type(x) == my.object.kind #3.x
    

    If you want to test all in the list:

    if any(isinstance(x, my.object.kind) for x in alist)
    

提交回复
热议问题