isinstance() and issubclass() return conflicting results

后端 未结 4 1942
既然无缘
既然无缘 2020-12-16 10:39
>>> class Hello:
    pass

and

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


        
4条回答
  •  囚心锁ツ
    2020-12-16 11:45

    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.

提交回复
热议问题