Checking the type

Michael Spencer spencerm at
Thu Mar 22 17:50:20 CET 2001

Markus Reitz <Markus_Reitz at> wrote in message
news:99a493$bhl$1 at
> Hi,
> is there a possibilty to check if a variable refers to an object of type
> Does a typeof-command exist in Python:
>   if a typeof list:
>      #Commands only useable with list
> I read the Python Tutorial, but have not found a hint to this topic.
> Thanks
> Markus

Sample code:

     def Show (self, object):
          """ <object> can be number, string, list, tuple, or dictionary"""
          from types import *
          kind = type (object)
          if kind == StringType:
              self.Writeln (object)
          elif (kind == ListType) or (kind == TupleType):
               for item in object:
                    self.Writeln (item)
          elif kind == DictType:
               sKeys = object.keys()
               for key in sKeys:
                    self.Writeln ('%-16s   %s' % (key, `object[key]`))
              self.Writeln (`object`)



More information about the Python-list mailing list