Skip to content
Advertisement

isinstance() and issubclass() return conflicting results

How do you explain isinstance(Hello,object) returns True whilst issubclass(Hello,object) returns False?

>>> class Hello:
    pass

and

>>> isinstance(Hello,object)
True
>>> issubclass(Hello,object)
False
>>> a = Hello()
>>> isinstance(a,object)
True

Advertisement

Answer

It’s because you are using old-style classes so it doesn’t derive from object. Try this instead:

class Hello(object):
    pass

>>> issubclass(Hello,object)
True

Old-style classes are deprecated and you shouldn’t use them any more.

In Python 3.x all classes are new-style and writing (object) is no longer required.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement