user1050619
user1050619

Reputation: 20856

isinstance operator returning false on inheritance

I have created 2 classes A & B, B inherits A. I'm using the isinstance to check if b is of type a and it returns false. Shouldn't be true?

class a():pass

class b(a):pass

print isinstance(b,a)

Upvotes: 3

Views: 1453

Answers (2)

Chocimier
Chocimier

Reputation: 48

b is class, not object, so it is not instance of any class. To get True, call isinstance(b(),a)

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

No. b is an instance of either type or classobj, not of a. You may want the issubclass function instead.

>>> issubclass(b, a)
True

Upvotes: 7

Related Questions