testing type of an object

Jonathan Hogg jonathan at onegoodidea.com
Fri Jun 28 16:20:26 EDT 2002


On 28/6/2002 20:00, in article afibp0$ekn25$1 at ID-76829.news.dfncis.de,
"Russell Blau" <russblau at hotmail.com> wrote:

> 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).

Or, in Python 2.2, it's preferable to compare against the new type
constructor builtins:

>>> type('foo') is str
1
>>> type(5) is int
1
>>> 

or:

>>> isinstance( 'foo', str )
1
>>> isinstance( 5, int )
1
>>> 

The other ones are: 'dict', 'long', 'list', and 'unicode'.

Jonathan




More information about the Python-list mailing list