0

Could someone please explain the following behaviour difference?

class a():
    pass
class b(a):
    pass

In Python2:

>>> isinstance(b(),type(a()))
True
>>> isinstance(a(),type(b()))
True

In Python3:

>>> isinstance(b(), type(a()))
True
>>> isinstance(a(), type(b()))
False
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Ryan
  • 219
  • 3
  • 12
  • 3
    Those are *old-style classes* in Python 2.x. – jonrsharpe Nov 23 '17 at 19:27
  • 1
    Also, there is no need to `type` the instance, just use the class as such, I mean, e.g. `isinstance(b(), a)`. There will be no differences. – keepAlive Nov 23 '17 at 19:30
  • OK, Thanks for the quick reply. I'm cleaning up code for bicop as I need it to work in Py3. He has a test that compares the classes of the old style, how do I make that test pass? or is it just as @Kanak says there is no need for type – Ryan Nov 23 '17 at 19:34
  • 1
    You can either do as I say or simply use the new-style class, i.e. `class a(object):pass`. There will be no differences neither. – keepAlive Nov 23 '17 at 19:40

0 Answers0