数字协议¶
-
int PyNumber_Check(PyObject *o)¶
- Part of the Stable ABI.
如果对象 o 提供数字的协议,返回真
1,否则返回假。这个函数不会调用失败。在 3.8 版更改: 如果 o 是一个索引整数则返回
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
NULLon 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
NULLon 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 得到的余数,如果失败,返回
NULL。等价于 Python 表达式o1 % o2。
-
PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2)¶
- Return value: New reference. Part of the Stable ABI.
参考内置函数
divmod()。如果失败,返回NULL。等价于 Python 表达式divmod(o1, o2)。
-
PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)¶
- Return value: New reference. Part of the Stable ABI.
请参阅内置函数
pow()。 如果失败,返回NULL。 等价于 Python 中的表达式pow(o1, o2, o3),其中 o3 是可选的。如果要忽略 o3,则需传入Py_None作为代替(如果传入NULL会导致非法内存访问)。
-
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
NULLon 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 得到的余数,如果失败,返回
NULL。当 o1 支持时,这个运算直接使用它储存结果。 等价于 Python 语句o1 %= o2。
-
PyObject *PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)¶
- Return value: New reference. Part of the Stable ABI.
请参阅内置函数
pow()。 如果失败,返回NULL。当 o1 支持时,这个运算直接使用它储存结果。当 o3 是Py_None时,等价于 Python 语句o1 **= o2;否则等价于在原来位置储存结果的pow(o1, o2, o3)。如果要忽略 o3,则需传入Py_None(传入NULL会导致非法内存访问)。
-
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 支持的前提下该操作将 原地 执行。 等价与 Python 语句o1 &= o2。
-
PyObject *PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)¶
- Return value: New reference. Part of the Stable ABI.
成功时返回 o1 和 o2 "按位异或的结果,失败时返回
NULL。 在 o1 支持的前提下该操作将 原地 执行。 等价与 Python 语句o1 ^= o2。
-
PyObject *PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)¶
- Return value: New reference. Part of the Stable ABI.
成功时返回 o1 和 o2 "按位或" 的结果,失败时返回
NULL。 在 o1 支持的前提下该操作将 原地 执行。 等价于 Python 语句o1 |= o2。
-
PyObject *PyNumber_Long(PyObject *o)¶
- Return value: New reference. Part of the Stable ABI.
成功时返回 o 转换为整数对象后的结果,失败时返回
NULL。 等价于 Python 表达式int(o)。
-
PyObject *PyNumber_Float(PyObject *o)¶
- Return value: New reference. Part of the Stable ABI.
成功时返回 o 转换为浮点对象后的结果,失败时返回
NULL。 等价于 Python 表达式float(o)。
-
PyObject *PyNumber_Index(PyObject *o)¶
- Return value: New reference. Part of the Stable ABI.
成功时返回 o 转换为 Python int 类型后的结果,失败时返回
NULL并引发TypeError异常。在 3.10 版更改: 结果总是为
int类型。 在之前版本中,结果可能为int的子类的实例。
-
PyObject *PyNumber_ToBase(PyObject *n, int base)¶
- Return value: New reference. Part of the Stable ABI.
返回整数 n 转换成以 base 为基数的字符串后的结果。这个 base 参数必须是 2,8,10 或者 16 。对于基数 2,8,或 16 ,返回的字符串将分别加上基数标识
'0b','0o', or'0x'。如果 n 不是 Python 中的整数 int 类型,就先通过PyNumber_Index()将它转换成整数类型。
-
Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)¶
- Part of the Stable ABI.
Returns o converted to a
Py_ssize_tvalue if o can be interpreted as an integer. If the call fails, an exception is raised and-1is returned.If o can be converted to a Python int but the attempt to convert to a
Py_ssize_tvalue would raise anOverflowError, then the exc argument is the type of exception that will be raised (usuallyIndexErrororOverflowError). If exc isNULL, then the exception is cleared and the value is clipped toPY_SSIZE_T_MINfor a negative integer orPY_SSIZE_T_MAXfor a positive integer.
-
int PyIndex_Check(PyObject *o)¶
- Part of the Stable ABI since version 3.8.
Returns
1if o is an index integer (has thenb_indexslot of thetp_as_numberstructure filled in), and0otherwise. This function always succeeds.