物件協定

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

None

Py_CONSTANT_FALSE

1

False

Py_CONSTANT_TRUE

2

True

Py_CONSTANT_ELLIPSIS

3

Ellipsis

Py_CONSTANT_NOT_IMPLEMENTED

4

NotImplemented

Py_CONSTANT_ZERO

5

0

Py_CONSTANT_ONE

6

1

Py_CONSTANT_EMPTY_STR

7

''

Py_CONSTANT_EMPTY_BYTES

8

b''

Py_CONSTANT_EMPTY_TUPLE

9

()

Numeric values are only given for projects which cannot use the constant identifiers.

在 3.13 版被加入.

在 CPython 中,所有這些常數都是不滅的。

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 NotImplemented singleton, used to signal that an operation is not implemented for the given type combination.

Py_RETURN_NOTIMPLEMENTED

Properly handle returning Py_NotImplemented from within a C function (that is, create a new strong reference to NotImplemented and return it).

Py_PRINT_RAW

Flag to be used with multiple functions that print the object (like PyObject_Print() and PyFile_WriteObject()). If passed, these functions use the str() of the object instead of the repr().

int PyObject_Print(PyObject *o, FILE *fp, int flags)

Print an object o, on file fp. Returns -1 on error. The flags argument is used to enable certain printing options. The only option currently supported is Py_PRINT_RAW; if given, the str() of the object is written instead of the repr().

int PyObject_HasAttrWithError(PyObject *o, PyObject *attr_name)
穩定 ABI 的一部分 自 3.13 版本開始.

Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(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 1 if o has the attribute attr_name, and 0 otherwise. This function always succeeds.

備註

Exceptions that occur when this calls __getattr__() and __getattribute__() methods aren't propagated, but instead given to sys.unraisablehook(). For proper error handling, use PyObject_HasAttrWithError(), PyObject_GetOptionalAttr() or PyObject_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 temporary str object are silently ignored. For proper error handling, use PyObject_HasAttrStringWithError(), PyObject_GetOptionalAttrString() or PyObject_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 NULL on failure. This is the equivalent of the Python expression o.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 raise AttributeError if the attribute is not found.

If the attribute is found, return 1 and set *result to a new strong reference to the attribute. If the attribute is not found, return 0 and set *result to NULL; the AttributeError is silenced. If an error other than AttributeError is raised, return -1 and set *result to NULL.

在 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_getattro slot. 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, an AttributeError is 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 -1 on failure; return 0 on success. This is the equivalent of the Python statement o.attr_name = v.

If v is NULL, the attribute is deleted. This behaviour is deprecated in favour of using PyObject_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 using PyObject_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() and PyObject_SetAttr() directly. For more details, see PyUnicode_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_setattro slot. 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, 0 is returned, otherwise an AttributeError is raised and -1 is returned.

int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
穩定 ABI 的一部分 自 3.13 版本開始.

Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del 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() and PyObject_DelAttr() directly. For more details, see PyUnicode_InternFromString(), which may be used internally to create a key object for lookup.

PyObject