luc luc - 4 months ago 7
Python Question

How does Python manage int and 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'>

Answer

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 int altogether and only having long.

Python 2: sys.maxint contains the maximum value a Python int can hold.

Python 3: sys.maxsize contains the maximum value a Python int can hold.

Comments