數(shù)字協(xié)議?
-
int PyNumber_Check(PyObject *o)?
- Part of the Stable ABI.
如果對象 o 提供數(shù)字的協(xié)議,返回真
1
,否則返回假。這個函數(shù)不會調(diào)用失敗。在 3.8 版更改: 如果 o 是一個索引整數(shù)則返回
1
。
-
PyObject *PyNumber_Add(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 、o2 相加的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 + o2
。
-
PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 減去 o2 的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 - o2
。
-
PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 、 o2 相乘的結果,如果失敗,返回
NULL
。等價于 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
。等價于 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 expressiono1 // 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 expressiono1 / o2
.
-
PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 除以 o2 得到的余數(shù),如果失敗,返回
NULL
。等價于 Python 表達式o1 % o2
。
-
PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
參考內(nèi)置函數(shù)
divmod()
。如果失敗,返回NULL
。等價于 Python 表達式divmod(o1, o2)
。
-
PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)?
- Return value: New reference. Part of the Stable ABI.
請參閱內(nèi)置函數(shù)
pow()
。 如果失敗,返回NULL
。 等價于 Python 中的表達式pow(o1, o2, o3)
,其中 o3 是可選的。如果要忽略 o3,則需傳入Py_None
作為代替(如果傳入NULL
會導致非法內(nèi)存訪問)。
-
PyObject *PyNumber_Negative(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
返回 o 的負值,如果失敗,返回
NULL
。等價于 Python 表達式-o
。
-
PyObject *PyNumber_Positive(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
返回 o,如果失敗,返回
NULL
。等價于 Python 表達式+o
。
-
PyObject *PyNumber_Absolute(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
返回 o 的絕對值,如果失敗,返回
NULL
。等價于 Python 表達式abs(o)
。
-
PyObject *PyNumber_Invert(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
返回 o 的按位取反后的結果,如果失敗,返回
NULL
。等價于 Python 表達式~o
。
-
PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 左移 o2 個比特后的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 << o2
。
-
PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 右移 o2 個比特后的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 >> o2
。
-
PyObject *PyNumber_And(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 和 o2 “按位與”的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 & o2
。
-
PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 和 o2 “按位異或”的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 ^ o2
。
-
PyObject *PyNumber_Or(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 和 o2 “按位或”的結果,如果失敗,返回
NULL
。等價于 Python 表達式o1 | o2
。
-
PyObject *PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 、o2 相加的結果,如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 Python 語句o1 += o2
。
-
PyObject *PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 、o2 相減的結果,如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 Python 語句o1 -= o2
。
-
PyObject *PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 、o2*相乘的結果,如果失敗,返回 ``NULL`` 。當 *o1 支持時,這個運算直接使用它儲存結果。 等價于 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 做矩陣乘法后的結果,如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 Python 語句o1 @= o2
。3.5 新版功能.
-
PyObject *PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 除以 o2 后向下取整的結果,如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 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 statemento1 /= o2
.
-
PyObject *PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 除以 o2 得到的余數(shù),如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 Python 語句o1 %= o2
。
-
PyObject *PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)?
- Return value: New reference. Part of the Stable ABI.
請參閱內(nèi)置函數(shù)
pow()
。 如果失敗,返回NULL
。當 o1 支持時,這個運算直接使用它儲存結果。當 o3 是Py_None
時,等價于 Python 語句o1 **= o2
;否則等價于在原來位置儲存結果的pow(o1, o2, o3)
。如果要忽略 o3,則需傳入Py_None
(傳入NULL
會導致非法內(nèi)存訪問)。
-
PyObject *PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 左移 o2 個比特后的結果,如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 Python 語句o1 <<= o2
。
-
PyObject *PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
返回 o1 右移 o2 個比特后的結果,如果失敗,返回
NULL
。當 o1 支持時,這個運算直接使用它儲存結果。 等價于 Python 語句o1 >>= o2
。
-
PyObject *PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
成功時返回 o1 和 o2 "按位與" 的結果,失敗時返回
NULL
。 在 o1 支持的前提下該操作將 原地 執(zhí)行。 等價與 Python 語句o1 &= o2
。
-
PyObject *PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
成功時返回 o1 和 o2 "按位異或的結果,失敗時返回
NULL
。 在 o1 支持的前提下該操作將 原地 執(zhí)行。 等價與 Python 語句o1 ^= o2
。
-
PyObject *PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)?
- Return value: New reference. Part of the Stable ABI.
成功時返回 o1 和 o2 "按位或" 的結果,失敗時返回
NULL
。 在 o1 支持的前提下該操作將 原地 執(zhí)行。 等價于 Python 語句o1 |= o2
。
-
PyObject *PyNumber_Long(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
成功時返回 o 轉(zhuǎn)換為整數(shù)對象后的結果,失敗時返回
NULL
。 等價于 Python 表達式int(o)
。
-
PyObject *PyNumber_Float(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
成功時返回 o 轉(zhuǎn)換為浮點對象后的結果,失敗時返回
NULL
。 等價于 Python 表達式float(o)
。
-
PyObject *PyNumber_Index(PyObject *o)?
- Return value: New reference. Part of the Stable ABI.
成功時返回 o 轉(zhuǎn)換為 Python int 類型后的結果,失敗時返回
NULL
并引發(fā)TypeError
異常。在 3.10 版更改: 結果總是為
int
類型。 在之前版本中,結果可能為int
的子類的實例。
-
PyObject *PyNumber_ToBase(PyObject *n, int base)?
- Return value: New reference. Part of the Stable ABI.
返回整數(shù) n 轉(zhuǎn)換成以 base 為基數(shù)的字符串后的結果。這個 base 參數(shù)必須是 2,8,10 或者 16 。對于基數(shù) 2,8,或 16 ,返回的字符串將分別加上基數(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 anOverflowError
, then the exc argument is the type of exception that will be raised (usuallyIndexError
orOverflowError
). If exc isNULL
, then the exception is cleared and the value is clipped toPY_SSIZE_T_MIN
for a negative integer orPY_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 thenb_index
slot of thetp_as_number
structure filled in), and0
otherwise. This function always succeeds.