type(x) syntax in a test

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Aug 23 06:18:24 EDT 2000


Bellamy Bruno wrote in comp.lang.python:
> Hi there...
> 
> I guess my mistake is a typical beginner's one, sorry...
> But I just can't find the right syntax to make a test on a type.
> 
> For instance:
> 
> a = 'hello world'
> if type(a) == 'string':
> 	print 'glad to meet you'
> 
> doesn't work...
> 
> I surely missed something, but so far I can't find out what...

Two typical uses:

import types
if type(a) == types.StringType:
   ...
if type(b) == types.IntType:
   
And:

if type(a) == type('x'):
   ...
if type(b) == type(3):
   ...
   
-- 
Remco Gerlich,  scarblac at pino.selwerd.nl

   This is no way to be
     Man ought to be free      -- Ted Bundy
       That man should be me



More information about the Python-list mailing list