Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if the given object is of given type in Python?

Tags:

python

types

I always thought operator is determined if the given variable is of the given type. But I just determined it was not true:

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

How do I test if a is of type class A?

Please advise.

Thanks, Boda Cydo.

like image 405
bodacydo Avatar asked Jan 23 '23 11:01

bodacydo


1 Answers

You want isinstance(a, A).

Keep in mind, it might be better to avoid the isinstance check by adding methods to A that make it do what you want without explicitly determining that it is an A.

is determines if two objects are the same object.

like image 141
Ned Batchelder Avatar answered Jan 30 '23 02:01

Ned Batchelder