我一直相信在 Python 解释器中x.__class__和的值type(x)是等价的。但是如果我们执行以下操作(在 Python 2.7、3.3 和 PyPy 2.0b1 中):
>>> import weakref
>>> x = set()
>>> y = weakref.proxy(x)
>>> x.__class__, isinstance(x, set), type(x)
(<type 'set'>, True, <type 'set'>)
>>> y.__class__, isinstance(y, set), type(y)
(<type 'set'>, True, <type 'weakproxy'>)
我们将看到它y.__class__对应于包装类型weakref.proxy(我想它weakref.proxy只是替换了伪装的属性)。甚至isinstance标识y为set.
但type显示“真实”类型 - weakproxy。所以,type不使用__class__属性来识别参数的类型,是吗?它是否为此目的使用了一些“更可靠”的来源?如果是这样,我们可以直接访问它吗?