How to check if variable is a specific class in python? -
i have variable "myvar" when print out type(myvar)
the output is:
<class 'my.object.kind'>
if have list of 10 variables including strings , variables of kind.. how can construct if statement check whether object in list "mylist" of <type 'my.object.kind'>
?
use isinstance
, return true if instance of subclass:
if isinstance(x, my.object.kind)
or:
type(x) == my.object.kind #3.x
if want test in list:
if any(isinstance(x, my.object.kind) x in alist)
Comments
Post a Comment