【发布时间】:2011-01-07 11:21:56
【问题描述】:
有人知道 Python 如何在内部管理 int 和 long 类型吗?
- 它是否动态选择正确的类型?
- int 的限制是多少?
- 我使用的是 Python 2.6,与以前的版本有什么不同吗?
我应该如何理解下面的代码?
>>> print type(65535)
<type 'int'>
>>> print type(65536*65536)
<type 'long'>
更新:
>>> print type(0x7fffffff)
<type 'int'>
>>> print type(0x80000000)
<type 'long'>
【问题讨论】:
-
在 CPython 中它们不只是动态映射到标准数据类型吗?
-
是的,我认为是的。我还怀疑一切都在堆上分配,所以当一个数字需要更高的精度时,他们只需
realloc就可以了。但我不太确定,所以我将答案留给其他人。 -
你也可以通过
var = 666L强制python使用long变量 -
@Ignacio: 错误 A CPython
int是 Clong(默认是有符号的)...见<CPython 2.X source>/Include/intobject.h: typedef struct { PyObject_HEAD long ob_ival; } PyIntObject;无论如何,Python 2.xint允许负数;一个 Cunsigned就是应付不过来。 -
PEP 237 讨论了 Python 如何在幕后使这一切看起来完全一样。