【发布时间】:2017-06-02 11:47:23
【问题描述】:
在下面的属性列表中,
>>> dir(object)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
__eq__、__ne__ 和 __hash__ 不显示为属性。它们是元类type的属性
>>> dir(type)
[... '__eq__', .... '__hash__', ... '__ne__', ...]
>>>
并且object 与is-a 不在type 关系中
>>> issubclass(object, type)
False
>>> issubclass(type, object)
True
但是,我看到这些属性是object 的一部分,
>>> object.__eq__
<method-wrapper '__eq__' of type object at 0x905b80>
>>> object.__ne__
<method-wrapper '__ne__' of type object at 0x905b80>
>>> object.__hash__
<slot wrapper '__hash__' of 'object' objects>
>>>
这允许,
class X(object):
pass
class X 覆盖这些属性。
问题:
这些是object的属性吗?
【问题讨论】:
-
type是object的“元类”。 :) -
isinstance(object, type)返回True -
@OluwafemiSule 嗯 1)
isinstance(X, type)也返回 true。object不是type的子类,但X是type的子类。这里发生了什么? 2)X为什么要继承type的属性
标签: python