Next / Previous / Contents / TCC Help System / NM Tech homepage

20.3. The types module

This module contains the type objects for all the different Python types. For any two objects of the same type or class, applying the type() function to them returns the same object:

>>> import types
>>> type(2.5)
<type 'float'>
>>> type(2.5) is types.FloatType
1

Here are most of the members of the types module. (A few more obscure types exist; see the Python Library Reference if you are working with the more exotic features of the language.)

BuiltinFunctionTypeBuilt-in function such as dir().
BuiltinMethodTypeBuilt-in method such as the .append() method on a list object.
ClassTypeUser-defined class.
ComplexTypeComplex number.
DictTypeDictionary.
FileTypeFile object.
FloatTypeFloating point number.
FunctionTypeUser-defined function.
GeneratorTypeGenerator (see generators above).
InstanceTypeInstance of a user-defined class.
IntTypeInteger.
ListTypeList.
LongTypeLong integer.
MethodTypeMethod of a user-defined class.
NoneTypeThe unique object None.
StringTypeString.
TupleTypeTuple.
TypeTypeType object.
UnicodeTypeUnicode string.
XRangeTypeResult returned by xrange().

In Python 2.2 and beyond, the name of the type's constructor function is the type object. This works for int, long, float, str (strings), unicode, tuple, list, and dict (dictionaries). For example:

>>>  type(2.5) is float
1