2

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)
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • Possible duplicate of [What is the difference between old style and new style classes in Python?](https://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-python) – anthony sottile Nov 23 '17 at 19:28

2 Answers2

7

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

>>> issubclass(b, a)
True
Jeremy
  • 1
  • 85
  • 340
  • 366
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

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

Chocimier
  • 48
  • 2
  • 5