testing type of an object

Russell Blau russblau at hotmail.com
Fri Jun 28 21:00:43 CEST 2002


"Rajarshi Guha" <rajarshi at presidency.com> wrote in message
news:pan.2002.06.28.14.49.33.858327.1787 at presidency.com...
> Hi,
>   I'm writing some code that depends on the type of an argument
> passed to it.
>
> Is there a standard trick to do this? My versions don't seem to work:
>
> if type(s) == 'str':
>   do something
>
> or
>
> if string.find(type(s), 'str') != -1:
>   do something
>
> dont seem to work

They don't work because the type() function returns a type _object_, not
a string containing the type name.

You can use

if type(s) == type('abc'):
    do something

or, even better, import the types module (which you can look up in the
docs).

--
I don't actually have a hotmail account; but I do have one on excite.com
if you really want to get in touch with me.






More information about the Python-list mailing list