types — Names for built-in types

This module defines names for some object types that are used by the standard Python interpreter, but not for the types defined by various extension modules. Also, it does not include some of the types that arise during processing such as the listiterator type. New names exported by future versions of this module will all end in Type.

Typical use is for functions that do different things depending on their argument types, like the following:

from types import IntType
def delete(mylist, item):
    if type(item) is IntType:
       del mylist[item]
    else:
       mylist.remove(item)

Starting in Python 2.2, built-in factory functions such as int() and str() are also names for the corresponding types. This is now the preferred way to access the type instead of using the types module. Accordingly, the example above should be written as follows:

def delete(mylist, item):
    if isinstance(item, int):
       del mylist[item]
    else:
       mylist.remove(item)

Starting in Python 3.0 all types that are also available as builtins are no longer exposed through the types module.

The module defines the following names:

types.FunctionType
types.LambdaType
The type of user-defined functions and lambdas.
types.GeneratorType
The type of generator-iterator objects, produced by calling a generator function.
types.CodeType

The type for code objects such as returned by compile().

types.MethodType
The type of methods of user-defined class instances.
types.BuiltinFunctionType
types.BuiltinMethodType
The type of built-in functions like len() or sys.exit().
types.ModuleType
The type of modules.
types.TracebackType
The type of traceback objects such as found in sys.exc_info()[2].
types.FrameType
The type of frame objects such as found in tb.tb_frame if tb is a traceback object.
types.GetSetDescriptorType
The type of objects defined in extension modules with PyGetSetDef, such as FrameType.f_locals or array.array.typecode. This type is used as descriptor for object attributes; it has the same purpose as the property type, but for classes defined in extension modules.
types.MemberDescriptorType
The type of objects defined in extension modules with PyMemberDef, such as datetime.timedelta.days. This type is used as descriptor for simple C data members which use standard conversion functions; it has the same purpose as the property type, but for classes defined in extension modules. In other implementations of Python, this type may be identical to GetSetDescriptorType.

Previous topic

weakref — Weak references

Next topic

copy — Shallow and deep copy operations

This Page

Quick search