[Tutor] question re type()

Aditya Lal aditya.n.lal at gmail.com
Sat Oct 27 15:52:25 CEST 2007


On 10/27/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
> "Dick Moores" <rdm at rcblue.com> wrote
>
> > if type(n) == 'int' or type(n) == 'long':
> >   do something
>
> don't use strings
>
> if type(n) == int
>
> Or just use an instance of the same type:
>
> if type(n) == type(42)
>
> Alan G.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

or use types module
import types

if type(n) == types.IntType or type(n) == types.LongType :
    blah!

this comes handy especially for some unique conditions - like whether the
function passed is a generator or a normal function (well I kinda had this
problem ... :) )

ex.
if type(f) == types.GeneratorType or type(f) == types.lambdaType :
   same blah!

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


More information about the Tutor mailing list