At 11:34 AM 10/27/2007, Dave Kuhlman wrote:
>On Sat, Oct 27, 2007 at 01:03:18PM +0100, Alan Gauld wrote:
>
> > if type(n) == int
> >
> > Or just use an instance of the same type:
> >
> > if type(n) == type(42)
>
>Calling type(n) for any integer seems to return the same object.
>I checked with id().
>
>So, should we be using:
>
>     if type(n) is type(42)
>
>or, as suggested by Aditya Lal in another message in this thread:
>
>     import types
>
>     if type(n) is types.IntType
>
>Or, is this a frivolous question that makes no difference?

I don't know. But lets see. We know this:
 >>> type(2147483648)
<type 'long'>
  >>> type(2147483647)
<type 'int'>
  >>>

Let's try:
=====================
#!/usr/bin/env python
#coding=utf-8
n = 2147483650
print n, type(n)
print
while n > 2147483645:
     if type(n) == type(10000000000):
         n -= 1
         print n, type(n)
     else:
         break
print
print n, type(n)
===================
This outputs
2147483650 <type 'long'>

2147483649 <type 'long'>
2147483648 <type 'long'>
2147483647 <type 'long'>
2147483646 <type 'long'>
2147483645 <type 'long'>

2147483645 <type 'long'>

And also try:
=====================
#!/usr/bin/env python
#coding=utf-8
import types
n = 2147483650
print n, type(n)
print
while n > 2147483645:
     if type(n) is types.LongType:
         n -= 1
         print n, type(n)
     else:
         break
print
print n, type(n)
==========================
which outputs
2147483650 <type 'long'>

2147483649 <type 'long'>
2147483648 <type 'long'>
2147483647 <type 'long'>
2147483646 <type 'long'>
2147483645 <type 'long'>

2147483645 <type 'long'>

Dick


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to