blob: e3cbef5ad020be680cae92dac79578700fbdc952 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`types` --- Names for built-in types
3=========================================
4
5.. module:: types
6 :synopsis: Names for built-in types.
7
8
9This module defines names for some object types that are used by the standard
10Python interpreter, but not for the types defined by various extension modules.
11Also, it does not include some of the types that arise during processing such as
Collin Winter2befd242007-08-28 06:09:03 +000012the ``listiterator`` type. New names exported by future versions of this module
13will all end in ``Type``.
Georg Brandl116aa622007-08-15 14:28:22 +000014
15Typical use is for functions that do different things depending on their
16argument types, like the following::
17
Collin Winter2befd242007-08-28 06:09:03 +000018 from types import IntType
Georg Brandl116aa622007-08-15 14:28:22 +000019 def delete(mylist, item):
20 if type(item) is IntType:
21 del mylist[item]
22 else:
23 mylist.remove(item)
24
25Starting in Python 2.2, built-in factory functions such as :func:`int` and
26:func:`str` are also names for the corresponding types. This is now the
27preferred way to access the type instead of using the :mod:`types` module.
28Accordingly, the example above should be written as follows::
29
30 def delete(mylist, item):
31 if isinstance(item, int):
32 del mylist[item]
33 else:
34 mylist.remove(item)
35
Christian Heimesc9543e42007-11-28 08:28:28 +000036Starting in Python 3.0 all types that are also available as builtins are no
37longer exposed through the types module.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039The module defines the following names:
40
Georg Brandl116aa622007-08-15 14:28:22 +000041.. data:: FunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000042 LambdaType
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 The type of user-defined functions and lambdas.
45
46
Georg Brandl116aa622007-08-15 14:28:22 +000047.. data:: GeneratorType
48
Georg Brandl9afde1c2007-11-01 20:32:30 +000049 The type of :term:`generator`-iterator objects, produced by calling a
50 generator function.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. data:: CodeType
54
55 .. index:: builtin: compile
56
57 The type for code objects such as returned by :func:`compile`.
58
59
Georg Brandl116aa622007-08-15 14:28:22 +000060.. data:: MethodType
61
62 The type of methods of user-defined class instances.
63
64
Georg Brandl116aa622007-08-15 14:28:22 +000065.. data:: BuiltinFunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000066 BuiltinMethodType
Georg Brandl116aa622007-08-15 14:28:22 +000067
68 The type of built-in functions like :func:`len` or :func:`sys.exit`.
69
70
Georg Brandl116aa622007-08-15 14:28:22 +000071.. data:: ModuleType
72
73 The type of modules.
74
75
Georg Brandl116aa622007-08-15 14:28:22 +000076.. data:: TracebackType
77
78 The type of traceback objects such as found in ``sys.exc_info()[2]``.
79
80
81.. data:: FrameType
82
83 The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
84 traceback object.
85
86
Georg Brandl116aa622007-08-15 14:28:22 +000087.. data:: GetSetDescriptorType
88
Christian Heimes5e696852008-04-09 08:37:03 +000089 The type of objects defined in extension modules with ``PyGetSetDef``, such
90 as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
91 descriptor for object attributes; it has the same purpose as the
92 :class:`property` type, but for classes defined in extension modules.
Georg Brandl116aa622007-08-15 14:28:22 +000093
Georg Brandl116aa622007-08-15 14:28:22 +000094
95.. data:: MemberDescriptorType
96
Christian Heimes5e696852008-04-09 08:37:03 +000097 The type of objects defined in extension modules with ``PyMemberDef``, such
98 as ``datetime.timedelta.days``. This type is used as descriptor for simple C
99 data members which use standard conversion functions; it has the same purpose
100 as the :class:`property` type, but for classes defined in extension modules.
101 In other implementations of Python, this type may be identical to
102 ``GetSetDescriptorType``.