IsString

Duncan Booth duncan.booth at invalid.invalid
Tue Dec 13 07:38:04 EST 2005


Gary Herron wrote:

> In Python, all values have an associated type which you can query with 
> type(v) thus:
> 
>   from types import *
> 
>   if type(v) == IntType:
>       ... whatever ...
> 
> Several types would qualify as "numbers":  IntType, FloatType, LongType, 
> and ComplexType,
> and several as "strings":  StringType and UnicodeType

This is bad practice on so many levels...

If you really do need to test for a type use isinstance, not 
'type(v)==something'.

You didn't need to import any of those types from types: they are all 
builtins albeit under different names.

If you really must:

   if isinstance(v, (int, float, long, complex)):
      ...

or for strings use the base type:

   if isinstance(v, basestring):
      ...



More information about the Python-list mailing list