When the value of an number goes beyond the integer range, python promotes it to a long. But when the value comes back to the integer range, why doesn’t it get demoted to an int?
>>> i=2147483647
>>> type(i)
<type 'int'>
>>> i = i + 1
>>> type(i)
<type 'long'>
>>> i = i - 10
>>> type(i)
<type 'long'>
>>> i
2147483638L
>>>
From the python source (in file Objects/longobject.c):
Note that the return types of both procedures are
PyLongObject *.What this shows is that addition and subtraction of
longs yield morelongs in python, regardless of whether the values could fit in ints.Example:
And here are python’s coercion rules, specifically:
So doing
i - 10, whereiis along, results in anotherlong.