bodacydo
bodacydo

Reputation: 79509

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

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.

Upvotes: 4

Views: 222

Answers (2)

unutbu
unutbu

Reputation: 880677

isinstance(a,A)

Upvotes: 2

Ned Batchelder
Ned Batchelder

Reputation: 375922

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.

Upvotes: 7

Related Questions