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'>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.maxintcontains the maximum value a Python int can hold.- On a 64-bit Python 2.7, the size is 24 bytes. Check with
sys.getsizeof().
- On a 64-bit Python 2.7, the size is 24 bytes. Check with
- Python 3:
sys.maxsizecontains 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.
Method 2
This PEP should help.
Bottom line is that you really shouldn’t have to worry about it in python versions > 2.4
Method 3
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.
Method 4
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.
Method 5
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.
Method 6
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'>
Method 7
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.
Method 8
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.
Method 9
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0