问题:Python如何管理int和long?

有人知道Python如何在内部管理int和long类型吗?

  • 它会动态选择合适的类型吗?
  • 一个整数的限制是多少?
  • 我正在使用Python 2.6,与以前的版本有所不同吗?

我应该如何理解以下代码?

>>> print type(65535)
<type 'int'>
>>> print type(65536*65536)
<type 'long'>

更新:

>>> print type(0x7fffffff)
<type 'int'>
>>> print type(0x80000000)
<type 'long'>

Does anybody know how Python manage internally int and long types?

  • Does it choose the right type dynamically?
  • What is the limit for an int?
  • I am using Python 2.6, Is is different with previous versions?

How should I understand the code below?

>>> print type(65535)
<type 'int'>
>>> print type(65536*65536)
<type 'long'>

Update:

>>> print type(0x7fffffff)
<type 'int'>
>>> print type(0x80000000)
<type 'long'>

回答 0

intlong“统一” 了几个版本。在此之前,可以通过数学运算来溢出int。

3.x通过完全消除long而仅具有int来进一步提高了此功能。

  • Python 2sys.maxint包含Python int可以容纳的最大值。
    • 在64位Python 2.7上,大小为24个字节。用检查sys.getsizeof()
  • Python 3sys.maxsize包含Python int可以达到的最大字节数。
    • 这将是32位的千兆字节和64位的EB。
    • 如此大的int的值将等于8的幂sys.maxsize

int and long were “unified” a few versions back. Before that it was possible to overflow an int through math ops.

3.x has further advanced this by eliminating long altogether and only having int.

  • Python 2: sys.maxint contains the maximum value a Python int can hold.
    • On a 64-bit Python 2.7, the size is 24 bytes. Check with sys.getsizeof().
  • Python 3: sys.maxsize contains the maximum size in bytes a Python int can be.
    • This will be gigabytes in 32 bits, and exabytes in 64 bits.
    • Such a large int would have a value similar to 8 to the power of sys.maxsize.

回答 1

PEP应该有所帮助。

最重要的是,在python版本> 2.4中,您真的不必担心它

This PEP should help.

Bottom line is that you really shouldn’t have to worry about it in python versions > 2.4


回答 2

在我的机器上:

>>> print type(1<<30)
<type 'int'>
>>> print type(1<<31)
<type 'long'>
>>> print type(0x7FFFFFFF)
<type 'int'>
>>> print type(0x7FFFFFFF+1)
<type 'long'>

Python使用整数(32位带符号整数,我不知道它们是否是C整数)适合适合32位的值,但是对于任何东西,它都会自动切换为长整数(任意大的位数,即bignums)更大。我猜想这将加快速度以提供较小的值,同时通过无缝过渡到bignum避免任何溢出。

On my machine:

>>> print type(1<<30)
<type 'int'>
>>> print type(1<<31)
<type 'long'>
>>> print type(0x7FFFFFFF)
<type 'int'>
>>> print type(0x7FFFFFFF+1)
<type 'long'>

Python uses ints (32 bit signed integers, I don’t know if they are C ints under the hood or not) for values that fit into 32 bit, but automatically switches to longs (arbitrarily large number of bits – i.e. bignums) for anything larger. I’m guessing this speeds things up for smaller values while avoiding any overflows with a seamless transition to bignums.


回答 3

有趣。在我的64位(i7 Ubuntu)盒子上:

>>> print type(0x7FFFFFFF)
<type 'int'>
>>> print type(0x7FFFFFFF+1)
<type 'int'>

猜猜它在更大的机器上提升到64位整数。

Interesting. On my 64-bit (i7 Ubuntu) box:

>>> print type(0x7FFFFFFF)
<type 'int'>
>>> print type(0x7FFFFFFF+1)
<type 'int'>

Guess it steps up to 64 bit ints on a larger machine.


回答 4

Python 2.7.9自动提升数字。对于不确定使用int()或long()的情况。

>>> a = int("123")
>>> type(a)
<type 'int'>
>>> a = int("111111111111111111111111111111111111111111111111111")
>>> type(a)
<type 'long'>

Python 2.7.9 auto promotes numbers. For a case where one is unsure to use int() or long().

>>> a = int("123")
>>> type(a)
<type 'int'>
>>> a = int("111111111111111111111111111111111111111111111111111")
>>> type(a)
<type 'long'>

回答 5

Python 2将根据值的大小自动设置类型。最大值指南可在下面找到。

Python 2中默认Int的Max值为65535,任何高于此值的值都会很长

例如:

>> print type(65535)
<type 'int'>
>>> print type(65536*65536)
<type 'long'>

在Python 3中,长数据类型已被删除,所有整数值都由Int类处理。Int的默认大小将取决于您的CPU体系结构。

例如:

  • 32位系统,整数的默认数据类型为’Int32′
  • 64位系统,整数的默认数据类型将为’Int64′

每种类型的最小值/最大值可在下面找到:

  • Int8:[-128,127]
  • Int16:[-32768,32767]
  • Int32:[-2147483648,2147483647]
  • Int64:[-9223372036854775808,9223372036854775807]
  • Int128:[-170141183460469231731687303715884105728,170141183460469231731687303715884105727]
  • UInt8:[0,255]
  • UInt16:[0,65535]
  • UInt32:[0,4294967295]
  • UInt64:[0,18446744073709551615]
  • UInt128:[0,340282366920938938463463374607431768211455]

如果您的Int大小超过上述限制,python将自动更改其类型并分配更多内存以处理此最小值/最大值的增加。在Python 2中,它将转换为“ long”,现在仅转换为下一个Int大小。

示例:如果您使用的是32位操作系统,则Int的最大值默认为2147483647。如果分配的值为2147483648或更大,则类型将更改为Int64。

有多种方法可以检查int的大小及其内存分配。注意:在Python 3中,<class 'int'>无论您使用的是什么Int大小,使用内置的type()方法总是会返回。

Python 2 will automatically set the type based on the size of the value. A guide of max values can be found below.

The Max value of the default Int in Python 2 is 65535, anything above that will be a long

For example:

>> print type(65535)
<type 'int'>
>>> print type(65536*65536)
<type 'long'>

In Python 3 the long datatype has been removed and all integer values are handled by the Int class. The default size of Int will depend on your CPU architecture.

For example:

  • 32 bit systems the default datatype for integers will be ‘Int32’
  • 64 bit systems the default datatype for integers will be ‘Int64’

The min/max values of each type can be found below:

  • Int8: [-128,127]
  • Int16: [-32768,32767]
  • Int32: [-2147483648,2147483647]
  • Int64: [-9223372036854775808,9223372036854775807]
  • Int128: [-170141183460469231731687303715884105728,170141183460469231731687303715884105727]
  • UInt8: [0,255]
  • UInt16: [0,65535]
  • UInt32: [0,4294967295]
  • UInt64: [0,18446744073709551615]
  • UInt128: [0,340282366920938463463374607431768211455]

If the size of your Int exceeds the limits mentioned above, python will automatically change it’s type and allocate more memory to handle this increase in min/max values. Where in Python 2, it would convert into ‘long’, it now just converts into the next size of Int.

Example: If you are using a 32 bit operating system, your max value of an Int will be 2147483647 by default. If a value of 2147483648 or more is assigned, the type will be changed to Int64.

There are different ways to check the size of the int and it’s memory allocation. Note: In Python 3, using the built-in type() method will always return <class 'int'> no matter what size Int you are using.


回答 6

从python 3.x开始,统一整数库比旧版本更加智能。在(i7 Ubuntu)盒子上,我得到了以下信息:

>>> type(math.factorial(30))
<class 'int'>

有关实现的详细信息,请参见Include/longintrepr.h, Objects/longobject.c and Modules/mathmodule.c文件。最后一个文件是动态模块(编译为so文件)。该代码很好地遵循。

From python 3.x, the unified integer libries are even more smarter than older versions. On my (i7 Ubuntu) box I got the following,

>>> type(math.factorial(30))
<class 'int'>

For implementation details refer Include/longintrepr.h, Objects/longobject.c and Modules/mathmodule.c files. The last file is a dynamic module (compiled to an so file). The code is well commented to follow.


回答 7

只是为了继续这里给出的所有答案,尤其是@James Lanes

整数类型的大小可以通过以下公式表示:

总范围=(2 ^位系统)

下限=-(2 ^位系统)* 0.5上限=((2 ^位系统)* 0.5)-1

Just to continue to all the answers that were given here, especially @James Lanes

the size of the integer type can be expressed by this formula:

total range = (2 ^ bit system)

lower limit = -(2 ^ bit system)*0.5 upper limit = ((2 ^ bit system)*0.5) – 1


回答 8

它管理着他们,因为intlong是同级类定义。它们具有用于+,-,*,/等的适当方法,这些方法将产生适当类的结果。

例如

>>> a=1<<30
>>> type(a)
<type 'int'>
>>> b=a*2
>>> type(b)
<type 'long'>

在这种情况下,该类int具有一个__mul__方法(实现*的方法),该方法可long在需要时创建结果。

It manages them because int and long are sibling class definitions. They have appropriate methods for +, -, *, /, etc., that will produce results of the appropriate class.

For example

>>> a=1<<30
>>> type(a)
<type 'int'>
>>> b=a*2
>>> type(b)
<type 'long'>

In this case, the class int has a __mul__ method (the one that implements *) which creates a long result when required.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。