[Tutor] More type() puzzlement

Aditya Lal aditya.n.lal at gmail.com
Sat Oct 27 17:39:33 CEST 2007


On 10/27/07, Dick Moores <rdm at rcblue.com> wrote:
>
> Win XP, Python 2.5.1
>
> ========================
> #!/usr/bin/env python
> #coding=utf-8
>
> n = 10000000000 # 10 billion
> print "type of 10 billion is", type(n)
> n = 1000000000 # 1 billion
> print "type of 1 billion is", type(n)
>
> raw_input("press enter to continue")
>
> n = 10000000000
> print type(n)
> while True:
>      if type(n) == long:
>          n -= 1000000
>          print n, type(n)
>      else:
>          break
> print n
> print type(n), 'HERE'
> ==========================
>
> As an exercise in using type() I was thinking I could use it to begin
> to find where (without looking it up) a long becomes an int. I show
> that the boundary is somewhere between 10 billion and 1 billion. But
> the above script never ends--never gets to 'HERE'.
>
> Here's part of the output:
>
> 6000000 <type 'long'>
> 5000000 <type 'long'>
> 4000000 <type 'long'>
> 3000000 <type 'long'>
> 2000000 <type 'long'>
> 1000000 <type 'long'>
> 0 <type 'long'>
> -1000000 <type 'long'>
> -2000000 <type 'long'>
> -3000000 <type 'long'>
> -4000000 <type 'long'>
> -5000000 <type 'long'>
> -6000000 <type 'long'>
>
> Note that it shows even 1 million and 0 to be longs, whereas
> >>> type(1000000)
> <type 'int'>
> >>> type(0)
> <type 'int'>
> >>>
>
> What's going on?
>
> Thanks,
>
> Dick Moores
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Hi Dick,
I would expect that a type change will happen if there is a need. Hence if
type(n) is already long it does not have to get converted to int to
accommodate something small.

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

HTH
Aditya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071027/92818315/attachment.htm 


More information about the Tutor mailing list