`<attribute 'xxx' of 'C' objects>` 在 `C.__dict__` 的输出中是什么意思?

What does `<attribute 'xxx' of 'C' objects>` mean in the output of `C.__dict__`?

>>> class C:
...     pass
... 
>>> C.__dict__
mappingproxy({'__doc__': None, 
'__weakref__': <attribute '__weakref__' of 'C' objects>, 
'__dict__': <attribute '__dict__' of 'C' objects>, 
'__module__': '__main__'})

<attribute 'xxx' of 'C' objects>

为什么 C 的某些属性(例如 __doc____module__)未在 <attribute 'xxx' of 'C' objects> 中提及,而其他属性却在

中提及?


回复:"note: <attribute ..> is basically the repr of these descriptors"

为什么<attribute ..>没有显示在下面的例子中?

>>> class md:
...     def __get__(self, obj, owner):
...         return 3
... 

>>> class CC:
...     d=md()
... 
>>> CC.__dict__
mappingproxy({'d': <__main__.md object at 0x7f7387f8c978>, ...})

这些是PyGetSetDescrObjects which are computed attributes (descriptors implemented in C) for instances of the class C. Documentation on these is minimal (or I can't seem to find it :-), but, you can take a look at tp_getset in the C-API,其中谈到了这些:

struct PyGetSetDef* PyTypeObject.tp_getset

An optional pointer to a static NULL-terminated array of PyGetSetDef structures, declaring computed attributes of instances of this type.

For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing a getset descriptor.

没有的对象 <attribute ..>(注意:<attribute ..>基本上就是这些描述符的repr)根本就不是描述符(如果未定义,__doc__ 通常是字符串或 None,而 __module__ 包含定义了 class 的模块的名称)。


Re: "note: <attribute ..> is basically the repr of these descriptors"

Why is <attribute ..> not shown in the following example?

解决我错过的这个更新。

因为这是在 Python 中实现的描述符,并且从 object.

继承了默认值 repr

C中实现的PyGetSetDescrObjectrepr函数使用了不同的repr,主要是为了区分它们(我假设)。