How to determine if a given object is a given type in Python?

I always thought that the is operator determines whether a given variable of a given type is set. But I just decided that it wasnโ€™t:

 >>> class A(): pass ... >>> a = A() >>> a is A False 

How to check if type a type class A ?

Please inform.

Thanks, Boda Sido.

+4
source share
2 answers

You want isinstance(a, A) .

Keep in mind that it is better to avoid checking isinstance by adding methods to A that force it to do what you want, without explicitly defining that it is A

is determines whether two objects are the same object.

+7
source
+2
source

Source: https://habr.com/ru/post/1303860/


All Articles