object 是 Python 2.X 类型的子类吗?

Is object a subclass of type in Python 2.X?

我从 Learning Python(第 5 版)中读到(第 1364 页,第 40 章):

In Python 2.X, new-style classes inherit from object, which is a subclass of type; classic classes are instances of type and are not created from a class.

然而,

issubclass(object, type)

给我

False

在 Python 2.7.

所以,作者似乎错误地陈述了 objecttype 的子类,还是我遗漏了什么?

使用isinstance()。在 python 2.7.10

print object
print isinstance(object, type)
print issubclass(object, type)
print object.__class__

产出

<type 'object'>
True
False
<type 'type'>

type 是元类 explained here

object 不是 type 的子 class,这会使它成为 metaclass。相反,object 是类型 type 实例

函数 issubclass 检查给定的 class 是否继承自另一个。

class A:
    pass

class B(A):
    pass

print(issubclass(B, A)) # True

它不检查给定类型的实例是否 os。要验证 object 是否确实属于 type 类型,您需要使用 isinstance.

print(isinstance(object, type)) # True