Checking the type
scarblac at pino.selwerd.nl
Wed Mar 21 13:25:23 CET 2001
Markus Reitz <Markus_Reitz at yahoo.com> wrote in comp.lang.python:
> is there a possibilty to check if a variable refers to an object of type x?
> 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.
There is a the type() function, and the list of types in the types module.
You can also use isinstance(), it's slightly more general.
>>> isinstance(3, types.IntType)
>>> class Spam:
>>> isinstance(Spam(), Spam)
In many cases, the Python way is to just assume that your input is a list,
and use it as if it were one. If it's not, an exception will be raised as
appropriate. This make it possible to make classes that are not lists but
behave like one (like UserList does). Same for files.
More information about the Python-list