數字協(xié)議?

int PyNumber_Check(PyObject *o)?
Part of the Stable ABI.

如果對象 o 提供數字的協(xié)議,返回真 1,否則返回假。這個(gè)函數不會(huì )調用失敗。

在 3.8 版更改: 如果 o 是一個(gè)索引整數則返回 1。

PyObject *PyNumber_Add(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 、o2 相加的結果,如果失敗,返回 NULL。等價(jià)于 Python 表達式 o1 + o2。

PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 減去 o2 的結果,如果失敗,返回 NULL。等價(jià)于 Python 表達式 o1 - o2。

PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 、 o2 相乘的結果,如果失敗,返回 NULL。等價(jià)于 Python 表達式 o1 * o2。

PyObject *PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI since version 3.7.

返回 o1 、o2 做矩陣乘法的結果,如果失敗,返回 NULL。等價(jià)于 Python 表達式 o1 @ o2。

3.5 新版功能.

PyObject *PyNumber_FloorDivide(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

Return the floor of o1 divided by o2, or NULL on failure. This is the equivalent of the Python expression o1 // o2.

PyObject *PyNumber_TrueDivide(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

Return a reasonable approximation for the mathematical value of o1 divided by o2, or NULL on failure. The return value is "approximate" because binary floating point numbers are approximate; it is not possible to represent all real numbers in base two. This function can return a floating point value when passed two integers. This is the equivalent of the Python expression o1 / o2.

PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 除以 o2 得到的余數,如果失敗,返回 NULL。等價(jià)于 Python 表達式 o1 % o2。

PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

參考內置函數 divmod()。如果失敗,返回 NULL。等價(jià)于 Python 表達式 divmod(o1, o2)。

PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)?
Return value: New reference. Part of the Stable ABI.

請參閱內置函數 pow()。 如果失敗,返回 NULL。 等價(jià)于 Python 中的表達式 pow(o1, o2, o3),其中 o3 是可選的。如果要忽略 o3,則需傳入 Py_None 作為代替(如果傳入 NULL 會(huì )導致非法內存訪(fǎng)問(wèn))。

PyObject *PyNumber_Negative(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

返回 o 的負值,如果失敗,返回 NULL 。等價(jià)于 Python 表達式 -o。

PyObject *PyNumber_Positive(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

返回 o,如果失敗,返回 NULL 。等價(jià)于 Python 表達式 +o。

PyObject *PyNumber_Absolute(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

返回 o 的絕對值,如果失敗,返回 NULL。等價(jià)于 Python 表達式 abs(o)。

PyObject *PyNumber_Invert(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

返回 o 的按位取反后的結果,如果失敗,返回 NULL。等價(jià)于 Python 表達式 ~o。

PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 左移 o2 個(gè)比特后的結果,如果失敗,返回 NULL。等價(jià)于 Python 表達式 o1 << o2。

PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 右移 o2 個(gè)比特后的結果,如果失敗,返回 NULL 。等價(jià)于 Python 表達式 o1 >> o2。

PyObject *PyNumber_And(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1o2 “按位與”的結果,如果失敗,返回 NULL 。等價(jià)于 Python 表達式 o1 & o2。

PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1o2 “按位異或”的結果,如果失敗,返回 NULL 。等價(jià)于 Python 表達式 o1 ^ o2。

PyObject *PyNumber_Or(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1o2 “按位或”的結果,如果失敗,返回 NULL 。等價(jià)于 Python 表達式 o1 | o2。

PyObject *PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 、o2 相加的結果,如果失敗,返回 NULL。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 += o2。

PyObject *PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 、o2 相減的結果,如果失敗,返回 NULL 。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 -= o2。

PyObject *PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 、o2*相乘的結果,如果失敗,返回 ``NULL`` 。當 *o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 *= o2。

PyObject *PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI since version 3.7.

返回 o1 、o2 做矩陣乘法后的結果,如果失敗,返回 NULL 。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 @= o2。

3.5 新版功能.

PyObject *PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 除以 o2 后向下取整的結果,如果失敗,返回 NULL。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 //= o2。

PyObject *PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

Return a reasonable approximation for the mathematical value of o1 divided by o2, or NULL on failure. The return value is "approximate" because binary floating point numbers are approximate; it is not possible to represent all real numbers in base two. This function can return a floating point value when passed two integers. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 /= o2.

PyObject *PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 除以 o2 得到的余數,如果失敗,返回 NULL。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 %= o2。

PyObject *PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)?
Return value: New reference. Part of the Stable ABI.

請參閱內置函數 pow()。 如果失敗,返回 NULL。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。當 o3Py_None 時(shí),等價(jià)于 Python 語(yǔ)句 o1 **= o2;否則等價(jià)于在原來(lái)位置儲存結果的 pow(o1, o2, o3) 。如果要忽略 o3,則需傳入 Py_None (傳入 NULL 會(huì )導致非法內存訪(fǎng)問(wèn))。

PyObject *PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 左移 o2 個(gè)比特后的結果,如果失敗,返回 NULL。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 <<= o2。

PyObject *PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

返回 o1 右移 o2 個(gè)比特后的結果,如果失敗,返回 NULL。當 o1 支持時(shí),這個(gè)運算直接使用它儲存結果。 等價(jià)于 Python 語(yǔ)句 o1 >>= o2。

PyObject *PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回 o1o2 "按位與" 的結果,失敗時(shí)返回 NULL。 在 o1 支持的前提下該操作將 原地 執行。 等價(jià)與 Python 語(yǔ)句 o1 &= o2。

PyObject *PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回 o1o2 "按位異或的結果,失敗時(shí)返回 NULL。 在 o1 支持的前提下該操作將 原地 執行。 等價(jià)與 Python 語(yǔ)句 o1 ^= o2。

PyObject *PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回 o1o2 "按位或" 的結果,失敗時(shí)返回 NULL。 在 o1 支持的前提下該操作將 原地 執行。 等價(jià)于 Python 語(yǔ)句 o1 |= o2。

PyObject *PyNumber_Long(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回 o 轉換為整數對象后的結果,失敗時(shí)返回 NULL。 等價(jià)于 Python 表達式 int(o)。

PyObject *PyNumber_Float(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回 o 轉換為浮點(diǎn)對象后的結果,失敗時(shí)返回 NULL。 等價(jià)于 Python 表達式 float(o)。

PyObject *PyNumber_Index(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回 o 轉換為 Python int 類(lèi)型后的結果,失敗時(shí)返回 NULL 并引發(fā) TypeError 異常。

在 3.10 版更改: 結果總是為 int 類(lèi)型。 在之前版本中,結果可能為 int 的子類(lèi)的實(shí)例。

PyObject *PyNumber_ToBase(PyObject *n, int base)?
Return value: New reference. Part of the Stable ABI.

返回整數 n 轉換成以 base 為基數的字符串后的結果。這個(gè) base 參數必須是 2,8,10 或者 16 。對于基數 2,8,或 16 ,返回的字符串將分別加上基數標識 '0b', '0o', or '0x'。如果 n 不是 Python 中的整數 int 類(lèi)型,就先通過(guò) PyNumber_Index() 將它轉換成整數類(lèi)型。

Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)?
Part of the Stable ABI.

Returns o converted to a Py_ssize_t value if o can be interpreted as an integer. If the call fails, an exception is raised and -1 is returned.

If o can be converted to a Python int but the attempt to convert to a Py_ssize_t value would raise an OverflowError, then the exc argument is the type of exception that will be raised (usually IndexError or OverflowError). If exc is NULL, then the exception is cleared and the value is clipped to PY_SSIZE_T_MIN for a negative integer or PY_SSIZE_T_MAX for a positive integer.

int PyIndex_Check(PyObject *o)?
Part of the Stable ABI since version 3.8.

Returns 1 if o is an index integer (has the nb_index slot of the tp_as_number structure filled in), and 0 otherwise. This function always succeeds.