[Tutor] More type() puzzlement

Dick Moores rdm at rcblue.com
Sat Oct 27 18:52:59 CEST 2007


At 08:39 AM 10/27/2007, Aditya Lal wrote:
>I would expect that a type change will happen if there is a need.

Hey, I HAD a need! OK, a made-up one.

>  Hence if type(n) is already long it does not have to get converted 
> to int to accommodate something small.

And that's not a bug?

>I changed your program to increase from 1B to 10B and the results 
>are as expected :)
>
><snip> - your old code
>
>n = 1000000000  # 1 billion
>print type(n)
>while n < 10000000000 :  # 10 billion
>     if type(n) == int:
>         n += 1000000
>         print n, type(n)
>     else :
>         break
>
><snip> - your old code

Thanks, Aditya.

Successively using greater and greater ints, and augmenting n by 
smaller and smaller amounts, I used finally

n = 2147483000
print n, type(n)
while n < 10000000000 :  # 10 billion
     if type(n) == int:
         n += 1
     else:
         print n, type(n)
         break

outputs
2147483000 <type 'int'>
2147483648 <type 'long'>

And then I can show this:
 >>> type(2147483648)
<type 'long'>
 >>> type(2147483647)
<type 'int'>
 >>>

So the largest int is 2147483647, or 2**31 - 1.
(I know this is all obvious to most, but I still get a kick out of doing it.)

Dick




More information about the Tutor mailing list