整数对象

所有整数被实现成任意尺寸的 long 整数对象。

当出错时,大多数 PyLong_As* API 返回 (return type)-1 不能区分数字。使用 PyErr_Occurred() 以消除歧义。

type PyLongObject
属于 Limited API (as an opaque struct).

此子类型的 PyObject 表示 Python 整数对象。

PyTypeObject PyLong_Type
属于 稳定 ABI (应用程序二进制接口) .

此实例的 PyTypeObject 表示 Python 整数类型。这是相同对象作为 int 在 Python 层。

int PyLong_Check ( PyObject * p )

返回 True 若其自变量是 PyLongObject 或子类型的 PyLongObject . This function always succeeds.

int PyLong_CheckExact ( PyObject * p )

返回 True 若其自变量是 PyLongObject ,但不是子类型的 PyLongObject . This function always succeeds.

PyObject * PyLong_FromLong ( long v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从 v ,或 NULL 当故障时。

当前实现保持整数对象数组对于所有整数介于 -5 and 256 . When you create an int in that range you actually just get back a reference to the existing object.

PyObject * PyLong_FromUnsignedLong ( unsigned long v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从 C unsigned long ,或 NULL 当故障时。

PyObject * PyLong_FromSsize_t ( Py_ssize_t v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从 C Py_ssize_t ,或 NULL 当故障时。

PyObject * PyLong_FromSize_t ( size_t v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从 C size_t ,或 NULL 当故障时。

PyObject * PyLong_FromLongLong ( long long v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从 C long long ,或 NULL 当故障时。

PyObject * PyLong_FromUnsignedLongLong ( unsigned long long v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从 C unsigned long long ,或 NULL 当故障时。

PyObject * PyLong_FromDouble ( double v )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 对象从整数部分的 v ,或 NULL 当故障时。

PyObject * PyLong_FromString ( const char * str , char * * pend , int base )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

返回新的 PyLongObject 基于字符串值 str , which is interpreted according to the radix in base ,或 NULL on failure. If pend 为非 NULL , *pend will point to the end of str on success or to the first character that could not be processed on error. If base is 0 , str is interpreted using the 整数文字 definition; in this case, leading zeros in a non-zero decimal number raises a ValueError 。若 base 不是 0 , it must be between 2 and 36 , inclusive. Leading and trailing whitespace and single underscores after a base specifier and between digits are ignored. If there are no digits or str is not NULL-terminated following the digits and trailing whitespace, ValueError 会被引发。

另请参阅

Python methods int.to_bytes() and int.from_bytes() to convert a PyLongObject to/from an array of bytes in base 256 . You can call those from C using PyObject_CallMethod() .

PyObject * PyLong_FromUnicodeObject ( PyObject * u , int base )
返回值:新引用。

转换 Unicode 数字序列在字符串中 u to a Python integer value.

Added in version 3.3.