數(shù)字協(xié)議?

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

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

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

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

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

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

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

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

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

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

返回 o1 、o2 做矩陣乘法的結(jié)果,如果失敗,返回 NULL。等價(jià)于 Python 表達(dá)式 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 得到的余數(shù),如果失敗,返回 NULL。等價(jià)于 Python 表達(dá)式 o1 % o2。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3.5 新版功能.

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

返回 o1 除以 o2 后向下取整的結(jié)果,如果失敗,返回 NULL。當(dāng) o1 支持時(shí),這個(gè)運(yùn)算直接使用它儲(chǔ)存結(jié)果。 等價(jià)于 Python 語句 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 得到的余數(shù),如果失敗,返回 NULL。當(dāng) o1 支持時(shí),這個(gè)運(yùn)算直接使用它儲(chǔ)存結(jié)果。 等價(jià)于 Python 語句 o1 %= o2。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.