物件協定¶
-
PyObject *Py_GetConstant(unsigned int constant_id)¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
Get a strong reference to a constant.
如果 constant_id 無效,則設定一個例外並回傳
NULL。constant_id 必須是這些常數識別字之一:
常數識別字
數值
回傳物件
-
Py_CONSTANT_NONE¶
0-
Py_CONSTANT_FALSE¶
1-
Py_CONSTANT_TRUE¶
2-
Py_CONSTANT_ELLIPSIS¶
3-
Py_CONSTANT_NOT_IMPLEMENTED¶
4-
Py_CONSTANT_ZERO¶
50-
Py_CONSTANT_ONE¶
61-
Py_CONSTANT_EMPTY_STR¶
7''-
Py_CONSTANT_EMPTY_BYTES¶
8b''-
Py_CONSTANT_EMPTY_TUPLE¶
9()Numeric values are only given for projects which cannot use the constant identifiers.
在 3.13 版被加入.
在 CPython 中,所有這些常數都是不滅的。
-
Py_CONSTANT_NONE¶
-
PyObject *Py_GetConstantBorrowed(unsigned int constant_id)¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
Similar to
Py_GetConstant(), but return a borrowed reference.This function is primarily intended for backwards compatibility: using
Py_GetConstant()is recommended for new code.The reference is borrowed from the interpreter, and is valid until the interpreter finalization.
在 3.13 版被加入.
-
PyObject *Py_NotImplemented¶
The
NotImplementedsingleton, used to signal that an operation is not implemented for the given type combination.
-
Py_RETURN_NOTIMPLEMENTED¶
Properly handle returning
Py_NotImplementedfrom within a C function (that is, create a new strong reference toNotImplementedand return it).
-
Py_PRINT_RAW¶
Flag to be used with multiple functions that print the object (like
PyObject_Print()andPyFile_WriteObject()). If passed, these functions use thestr()of the object instead of therepr().
-
int PyObject_Print(PyObject *o, FILE *fp, int flags)¶
Print an object o, on file fp. Returns
-1on error. The flags argument is used to enable certain printing options. The only option currently supported isPy_PRINT_RAW; if given, thestr()of the object is written instead of therepr().
-
int PyObject_HasAttrWithError(PyObject *o, PyObject *attr_name)¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
Returns
1if o has the attribute attr_name, and0otherwise. This is equivalent to the Python expressionhasattr(o, attr_name). On failure, return-1.在 3.13 版被加入.
-
int PyObject_HasAttrStringWithError(PyObject *o, const char *attr_name)¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
This is the same as
PyObject_HasAttrWithError(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.在 3.13 版被加入.
-
int PyObject_HasAttr(PyObject *o, PyObject *attr_name)¶
- 為 穩定 ABI 的一部分.
Returns
1if o has the attribute attr_name, and0otherwise. This function always succeeds.備註
Exceptions that occur when this calls
__getattr__()and__getattribute__()methods aren't propagated, but instead given tosys.unraisablehook(). For proper error handling, usePyObject_HasAttrWithError(),PyObject_GetOptionalAttr()orPyObject_GetAttr()instead.
-
int PyObject_HasAttrString(PyObject *o, const char *attr_name)¶
- 為 穩定 ABI 的一部分.
This is the same as
PyObject_HasAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.備註
Exceptions that occur when this calls
__getattr__()and__getattribute__()methods or while creating the temporarystrobject are silently ignored. For proper error handling, usePyObject_HasAttrStringWithError(),PyObject_GetOptionalAttrString()orPyObject_GetAttrString()instead.
-
PyObject *PyObject_GetAttr(PyObject *o, PyObject *attr_name)¶
- 回傳值:新的參照。 為 穩定 ABI 的一部分.
Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or
NULLon failure. This is the equivalent of the Python expressiono.attr_name.If the missing attribute should not be treated as a failure, you can use
PyObject_GetOptionalAttr()instead.
-
PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)¶
- 回傳值:新的參照。 為 穩定 ABI 的一部分.
This is the same as
PyObject_GetAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.If the missing attribute should not be treated as a failure, you can use
PyObject_GetOptionalAttrString()instead.
-
int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result);¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
Variant of
PyObject_GetAttr()which doesn't raiseAttributeErrorif the attribute is not found.If the attribute is found, return
1and set *result to a new strong reference to the attribute. If the attribute is not found, return0and set *result toNULL; theAttributeErroris silenced. If an error other thanAttributeErroris raised, return-1and set *result toNULL.在 3.13 版被加入.
-
int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result);¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
This is the same as
PyObject_GetOptionalAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.在 3.13 版被加入.
-
PyObject *PyObject_GenericGetAttr(PyObject *o, PyObject *name)¶
- 回傳值:新的參照。 為 穩定 ABI 的一部分.
Generic attribute getter function that is meant to be put into a type object's
tp_getattroslot. It looks for a descriptor in the dictionary of classes in the object's MRO as well as an attribute in the object's__dict__(if present). As outlined in 實作描述器, data descriptors take preference over instance attributes, while non-data descriptors don't. Otherwise, anAttributeErroris raised.
-
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)¶
- 為 穩定 ABI 的一部分.
Set the value of the attribute named attr_name, for object o, to the value v. Raise an exception and return
-1on failure; return0on success. This is the equivalent of the Python statemento.attr_name = v.If v is
NULL, the attribute is deleted. This behaviour is deprecated in favour of usingPyObject_DelAttr(), but there are currently no plans to remove it.
-
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)¶
- 為 穩定 ABI 的一部分.
This is the same as
PyObject_SetAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.If v is
NULL, the attribute is deleted, but this feature is deprecated in favour of usingPyObject_DelAttrString().The number of different attribute names passed to this function should be kept small, usually by using a statically allocated string as attr_name. For attribute names that aren't known at compile time, prefer calling
PyUnicode_FromString()andPyObject_SetAttr()directly. For more details, seePyUnicode_InternFromString(), which may be used internally to create a key object.
-
int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)¶
- 為 穩定 ABI 的一部分.
Generic attribute setter and deleter function that is meant to be put into a type object's
tp_setattroslot. It looks for a data descriptor in the dictionary of classes in the object's MRO, and if found it takes preference over setting or deleting the attribute in the instance dictionary. Otherwise, the attribute is set or deleted in the object's__dict__(if present). On success,0is returned, otherwise anAttributeErroris raised and-1is returned.
-
int PyObject_DelAttr(PyObject *o, PyObject *attr_name)¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
Delete attribute named attr_name, for object o. Returns
-1on failure. This is the equivalent of the Python statementdel o.attr_name.
-
int PyObject_DelAttrString(PyObject *o, const char *attr_name)¶
- 為 穩定 ABI 的一部分 自 3.13 版本開始.
This is the same as
PyObject_DelAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.The number of different attribute names passed to this function should be kept small, usually by using a statically allocated string as attr_name. For attribute names that aren't known at compile time, prefer calling
PyUnicode_FromString()andPyObject_DelAttr()directly. For more details, seePyUnicode_InternFromString(), which may be used internally to create a key object for lookup.