描述器对象¶
“描述器”是描述对象的某些属性的对象。它们存在于类型对象的字典中。
-
PyObject *PyDescr_NewGetSet(PyTypeObject *type, struct PyGetSetDef *getset)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据
PyGetSetDef结构体 getset 为扩展类型 type 创建一个新的 get-set 描述器。get-set 描述器将暴露由 C getter 和 setter 函数实现的属性而不是直接存储在实例中。 这与为
tp_getset中条目创建的描述器种类相同,它将作为一个types.GetSetDescriptorType对象出现在 Python 中。成功时,返回指向该描述符的 强引用。 失败时返回
NULL并设置异常。
-
PyObject *PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *member)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据
PyMemberDef结构体 member 为扩展类型 type 创建一个新的成员描述符。成员描述符将类型 C 结构体中的字段公开为 Python 属性。 这与为
tp_members中的条目创建的描述符属于同一种类,并且在 Python 中显示为types.MemberDescriptorType对象。成功时,返回指向该描述符的 强引用。 失败时返回
NULL并设置异常。
-
PyTypeObject PyMemberDescr_Type¶
- 属于 稳定 ABI.
基于
PyMemberDef结构体创建的成员描述器对象的类型对象。这种描述器将 C 结构的字段暴露为类型的属性,对应 Python 中的types.MemberDescriptorType对象。
-
PyTypeObject PyGetSetDescr_Type¶
- 属于 稳定 ABI.
根据
PyGetSetDef结构体创建的 get/set 描述符对象的类型对象。 这些描述符实现了其值由 C getter 和 setter 函数计算的属性,并用于许多内置类型属性。 它们对应于 Python 中的types.GetSetDescriptorType对象。
-
PyObject *PyDescr_NewMethod(PyTypeObject *type, struct PyMethodDef *meth)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据
PyMethodDef结构体 meth 为扩展类型 type 创建一个新的方法描述符。方法描述符将 C 函数公开为类型上的方法。 这与为
tp_methods中的条目创建的描述符属于同一种类,并且在 Python 中显示为types.MethodDescriptorType对象。成功时,返回指向该描述符的 强引用。 失败时返回
NULL并设置异常。
-
PyTypeObject PyMethodDescr_Type¶
- 属于 稳定 ABI.
根据
PyMethodDef结构体创建的方法描述符对象的类型对象。 这些描述符将 C 函数公开为类型上的方法,并对应于 Python 中的types.MethodDescriptorType对象。
-
PyTypeObject PyWrapperDescr_Type¶
- 属于 稳定 ABI.
由
PyDescr_NewWrapper()和PyWrapper_New()创建的包装器描述器对象的类型对象。Wrapper descriptors are used internally to expose special methods such as
__repr__or__add__, which are implemented by type slots. The type is exposed in Python astypes.WrapperDescriptorType.
-
PyObject *PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)¶
- 返回值:新的引用。
Create a new wrapper descriptor of type
PyWrapperDescr_Type.This function is meant for internal use only; it and related API (
struct wrapperbaseand thePyWrapperFlag_KEYWORDSflag) are exposed publicly for backwards compatibility reasons.请参阅
PyCMethod_New()了解如何包装 C 函数。
-
PyObject *PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据
PyMethodDef结构体 method 为扩展类型 type 创建一个新的类方法描述符。类方法描述符公开在被访问时接收类而不是实例的 C 方法。 这与为
tp_methods中的METH_CLASS条目创建的描述符属于同一种类,并且在 Python 中显示为types.ClassMethodDescriptorType对象。成功时,返回指向该描述符的 强引用。 失败时返回
NULL并设置异常。
-
int PyDescr_IsData(PyObject *descr)¶
如果描述器对象 descr 描述的是一个数据属性则返回非零值,或者如果它描述的是一个方法则返回
0。 descr 必须是一个描述器对象;不会进行错误检查。
-
PyObject *PyWrapper_New(PyObject *d, PyObject *self)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据包装器描述符 d 和实例 self 创建一个新的绑定包装器对象。
这是由
PyDescr_NewWrapper()创建的包装器描述符的绑定形式。 当通过实例访问槽位包装器时,CPython 会创建这些对象,它们在 Python 中显示为types.MethodWrapperType对象。成功时,返回指向该包装器对象的 强引用。 失败时返回
NULL并设置异常。
-
PyDescr_TYPE(descr)¶
Return a borrowed reference to the type that the descriptor descr belongs to. This is accessible as
__objclass__in Python.descr must be a descriptor object created by one of the
PyDescr_New*functions (an instance ofPyMethodDescr_Type,PyClassMethodDescr_Type,PyMemberDescr_Type,PyGetSetDescr_Type, orPyWrapperDescr_Type). If the passed descriptor is not an instance of one of these types, the behavior is undefined.
-
PyDescr_NAME(descr)¶
Return the name of the descriptor descr as a borrowed reference to a
strobject. This is available as__name__in Python.descr must be a descriptor object created by one of the
PyDescr_New*functions. If not, the behavior is undefined.
-
PyDescr_COMMON¶
This is a macro including the common fields for a descriptor object.
This was included in Python's C API by mistake; do not use it in extensions. For creating custom descriptor objects, create a class implementing the descriptor protocol (
tp_descr_getandtp_descr_set).自 3.15 版起已处于 Soft deprecated 状态.
内置描述器¶
-
PyTypeObject PyProperty_Type¶
- 属于 稳定 ABI.
property 对象的类型对象。 它与 Python 层面的
property是相同的对象。
-
PyTypeObject PySuper_Type¶
- 属于 稳定 ABI.
super 对象的类型对象。它与 Python 层面的
super是相同的对象。
-
PyTypeObject PyClassMethod_Type¶
类方法对象的类型。它与 Python 层面的
classmethod是相同的对象。
-
PyTypeObject PyClassMethodDescr_Type¶
- 属于 稳定 ABI.
C 级别类方法描述符对象的类型对象。 这是为在 C 扩展类型中定义的
classmethod()创建的描述符的类型,对应于 Python 中的types.ClassMethodDescriptorType对象。
-
PyObject *PyClassMethod_New(PyObject *callable)¶
新建一个包裹 callable 的
classmethod对象。callable 必须是一个可调用对象,并且不可为NULL值。成功时,此函数将返回一个指向新类方法描述器的 strong reference。失败时,此函数将返回
NULL并设置一个异常。
-
PyTypeObject PyStaticMethod_Type¶
静态方法对象的类型。它与 Python 层面的
staticmethod是相同的对象。
-
PyObject *PyStaticMethod_New(PyObject *callable)¶
新建一个包裹 callable 的
staticmethod对象。callable 必须是一个可调用对象,并且不可为NULL值。成功时,此函数将返回一个指向新静态方法描述器的 strong reference。失败时,此函数将返回
NULL并设置一个异常。