[Tutor] a question about maxint
eryksun
eryksun at gmail.com
Wed Oct 3 13:33:50 CEST 2012
On Wed, Oct 3, 2012 at 1:28 AM, Katya Stolpovskaya
<katerina.sto at gmail.com> wrote:
>
> Thank you for you reply, but with "long" I got the same error:
>
>>>> from sys import *
>>>> long
>
> Traceback (most recent call last):
> File "<pyshell#5>", line 1, in <module>
> long
> NameError: name 'long' is not defined
I assumed some familiarity with Python 2, which has two integer
arithmetic types: int (fixed precision, using the platform's C long
type) and long (arbitrary precision). The naming is a bit conflated in
that a Python int uses a C long, which has nothing to do with a Python
long.
In Python 2.2+, arithmetic expressions with Python int types can
seamlessly return a Python long. For example, on a system with a
32-bit C long:
If the intermittent result never exceeds 2**31 - 1, the expression
returns an int:
>>> 2**30 - 1 + 2**30
2147483647
However, if the order of operations in this expression are swapped to
have an intermittent result of 2**31, it returns a long (the
representation ends with an 'L'):
>>> 2**30 + 2**30 - 1
2147483647L
Python 3, on the other hand, pretty much renamed "long" to "int" (as
it similarly renamed unicode to str), so now there's only one integer
type named "int". There is no built-in type named "long". But the C
API preserves the original type (PyLong_Type) and function names such
as PyLong_FromSsize_t:
>>> from ctypes import *
>>> type( pythonapi.PyLong_FromSsize_t(-1) )
<class 'int'>
PEP 237 (Unifying Long Integers and Integers) goes into more detail,
but it predates the Python 3 implementation:
http://www.python.org/dev/peps/pep-0237
More information about the Tutor
mailing list