代码之家  ›  专栏  ›  技术社区  ›  Tim unnamed eng

在“C.\uu dict\uuu2”的输出中,“C”对象的“属性”xxx”是什么意思?

  •  2
  • Tim unnamed eng  · 技术社区  · 8 年前
    >>> 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' objects C ?
    • <“C”对象的属性“xxx”> 意思是 xxx ? 但它被列为 C.__dict__ 作为的属性 .

    __doc__ __module__ )的 未提及 <“C”对象的属性“xxx”> ,而其他人呢?


    回复:“注: <attribute ..> 基本上是这些描述符的repr“

    <属性&燃气轮机; 以下示例中未显示?

    >>> class md:
    ...     def __get__(self, obj, owner):
    ...         return 3
    ... 
    
    >>> class CC:
    ...     d=md()
    ... 
    >>> CC.__dict__
    mappingproxy({'d': <__main__.md object at 0x7f7387f8c978>, ...})
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Community Mohan Dere    6 年前

    PyGetSetDescrObject s 这些是类实例的计算属性(用C实现的描述符) C Documentation on these tp_getset in the C-API

    struct PyGetSetDef* PyTypeObject.tp_getset

    指向PyGetSetDef结构的静态NULL终止数组的可选指针, .

    对于数组中的每个条目,都会将一个条目添加到包含getset描述符的类型字典(请参阅下面的tp\u dict)。

    <attribute ..> < 基本上是 repr __doc__ 通常是字符串或 None 如果未定义 __module__ 保存在其中定义类的模块的名称)。


    <属性&燃气轮机; 基本上是这些描述符的repr“

    为什么是 <属性&燃气轮机;

    来解决我错过的这个更新。

    因为这是用Python实现的描述符,并且继承了默认值 报告 object .

    这个 的功能 实施于 使用不同的 报告 ,主要是为了区分它们(我假设)。