企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](sequence.xhtml "Sequence Protocol") | - [上一页](object.xhtml "Object Protocol") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python/C API 参考手册](index.xhtml) » - [抽象对象层](abstract.xhtml) » - $('.inline-search').show(0); | # 数字协议 int `PyNumber_Check`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)如果对象 *o* 提供数字的协议,返回真 `1`,否则返回假。这个函数不会调用失败。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Add`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 、*o2\*相加的结果,如果失败,返回 \*NULL* 。等价于Python中的表达式 `o1 + o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Subtract`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 减去 *o2* 的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `o1 - o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Multiply`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回\*o1\* 、*o2\*相乘的结果,如果失败,返回 \*NULL* 。等价于Python中的表达式 `o1 * o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_MatrixMultiply`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回\*o1\* 、*o2\*做矩阵乘法的结果,如果失败,返回 \*NULL* 。等价于Python中的表达式 `o1 @ o2`。 3\.5 新版功能. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_FloorDivide`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 除以 *o2* 的向下取整后的结果,如果失败,返回 *NULL*。等价于"传统"的整数除法。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_TrueDivide`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 除以 *o2* 的一个合理的近似值,如果失败,返回 *NULL*。这个值是近似的是因为二进制浮点数是一个近似值,它不可能表示出以2为基数的所有实数。这个函数返回两个整数相除得到的浮点数。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Remainder`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 除以 *o2* 得到的余数,如果失败,返回 *NULL*。等价于Python中的表达式 `o1 % o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Divmod`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*参考内置函数 [`divmod()`](../library/functions.xhtml#divmod "divmod")。如果失败,返回 *NULL*。等价于Python中的表达式 `divmod(o1, o2)`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Power`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o3*)*Return value: New reference.*请参阅内置函数 [`pow()`](../library/functions.xhtml#pow "pow")。 如果失败,返回 *NULL*。 等价于 Python 中的表达式 `pow(o1, o2, o3)`,其中 *o3* 是可选的。如果缺少 *o3*,则传入 [`Py_None`](none.xhtml#c.Py_None "Py_None") 作为代替(如果传入 *NULL* 会导致非法内存访问)。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Negative`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 *o* 的负值,如果失败,返回 *NULL* 。等价于Python中的表达式 `-o`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Positive`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 *o* ,如果失败,返回 *NULL* 。等价于Python中的表达式 `+o`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Absolute`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 ”o“ 的绝对值,如果失败,返回 *NULL*。等价于Python中的表达式 `abs(o)`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Invert`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 *o* 的按位取反后的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `~o`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Lshift`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 左移 *o2* 个比特后的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `o1 << o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Rshift`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 右移 *o2* 个比特后的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `o1 >> o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_And`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 和 *o2* “按位与”的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `o1 & o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Xor`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 和 *o2* “按位异或”的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `o1 ^ o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Or`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 和 *o2* “按位或”的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `o1 | o2` [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceAdd`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 、*o2\*相加的结果,如果失败,返回 \*NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 += o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceSubtract`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 减去 *o2\*的结果,如果失败,返回 \*NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1* 。 等价于Python中的语句 `o1 -= o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceMultiply`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 、*o2\*相乘的结果,如果失败,返回 \*NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 *= o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceMatrixMultiply`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 、*o2\*做矩阵乘法后的结果,如果失败,返回 \*NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 @= o2`。 3\.5 新版功能. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceFloorDivide`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 除以 *o2* 后向下取整的结果,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 //= o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceTrueDivide`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 除以 *o2* 的一个合理的近似值,如果失败,返回 *NULL*。这个值是近似的是因为二进制浮点数是一个近似值,它不可能表示出以2为基数的所有实数。这个函数返回两个整数相除得到的浮点数。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceRemainder`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 除以 *o2* 得到的余数,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 %= o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlacePower`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o3*)*Return value: New reference.*请参阅内置函数 [`pow()`](../library/functions.xhtml#pow "pow")。 如果失败,返回 *NULL*。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。等价价于 Python 中的表达式 `pow(o1, o2, o3)`,其中 *o3* 是可选的。如果缺少 *o3*,则传入 [`Py_None`](none.xhtml#c.Py_None "Py_None") 作为代替(如果传入 *NULL* 会导致非法内存访问)。如果 *o3* 是 [`Py_None`](none.xhtml#c.Py_None "Py_None") , 则该函数等价于 Python 中的语句 `o1 **= o2` 。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceLshift`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 左移 *o2* 个比特后的结果,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 <<= o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceRshift`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 右移 *o2* 个比特后的结果,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 >>= o2`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceAnd`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 和 *o2* “按位与” 的结果,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 &= o2` [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceXor`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 和 *o2* “按位异或” 的结果,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 ^= o2` [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_InPlaceOr`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o1*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*o2*)*Return value: New reference.*返回 *o1* 和 *o2* “按位或” 的结果,如果失败,返回 *NULL* 。当 *o1* 支持时,这个运算将完成后的值赋给 *o1*。 等价于Python中的语句 `o1 |= o2` [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Long`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 *o* 转换成整数类型的对象后的结果,如果失败,返回 *NULL* 。等价于Python中的表达式 `int(o)`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Float`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 *o* 转换成浮点数类型的对象后的结果,如果失败,返回 *NULL*。等价于Python中的表达式 `float(o)`。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_Index`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回 *o* 转换为 Python 中的整数 *int* 类型后的结果,如果失败,返回 *NULL* 并且引发 [`TypeError`](../library/exceptions.xhtml#TypeError "TypeError") 异常 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyNumber_ToBase`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*n*, int *base*)*Return value: New reference.*返回整数 *n* 转换成以 *base* 为基数的字符串后的结果。这个 *base* 参数必须是 2,8,10 或者 16 。对于基数 2,8,或 16 ,返回的字符串将分别加上基数标识 `'0b'`, `'0o'`, or `'0x'`。如果 *n* 不是 Python 中的整数 *int* 类型,就先通过 [`PyNumber_Index()`](#c.PyNumber_Index "PyNumber_Index") 将它转换成整数类型。 Py\_ssize\_t `PyNumber_AsSsize_t`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)如果 *o* 是一个整数类型的解释型,返回 *o* 转换成一个 Py\_ssize\_t 值项后的结果。如果调用失败,返回 `-1` 并引发异常。 如果 *o* 可以被转换成 Python 中的整数 *int* 类型但是如果试图转换成一个 Py\_ssize\_t 值项则引发 [`OverflowError`](../library/exceptions.xhtml#OverflowError "OverflowError") ,同时 *exc* 参数是引发这个异常的类型(一般是 [`IndexError`](../library/exceptions.xhtml#IndexError "IndexError") 或者 [`OverflowError`](../library/exceptions.xhtml#OverflowError "OverflowError"))。如果 *exc* 是 *NULL*,那么这个异常被清除,同时,如果这个值项是正整数则它被省略成 *PY\_SSIZE\_T\_MIN*, 如果是负数则被省略成 *PY\_SSIZE\_T\_MAX* 。 int `PyIndex_Check`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)如果 *o* 是一个索引整数(存有 nb\_index 位置并有 tp\_as\_number 填入其中)则返回 `1`,否则返回 `0` 。这个函数不会调用失败。 ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](sequence.xhtml "Sequence Protocol") | - [上一页](object.xhtml "Object Protocol") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python/C API 参考手册](index.xhtml) » - [抽象对象层](abstract.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。