blob: ec271e4f504c5c0e9c540a2e30db672cb68cfbda [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
36The module defines the following names:
37
38
39.. data:: NoneType
40
41 The type of ``None``.
42
43
44.. data:: TypeType
Georg Brandla1e9ef72007-09-04 07:23:09 +000045 ClassType
Georg Brandl116aa622007-08-15 14:28:22 +000046
47 .. index:: builtin: type
48
Georg Brandla1e9ef72007-09-04 07:23:09 +000049 The type of type objects (such as returned by :func:`type`) and user-defined
50 classes without metaclass; alias of the built-in :class:`type`.
51
52
53.. data:: ObjectType
54
55 Alias of the built-in :func:`object`.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. data:: BooleanType
59
60 The type of the :class:`bool` values ``True`` and ``False``; alias of the
61 built-in :class:`bool`.
62
Georg Brandl116aa622007-08-15 14:28:22 +000063
64.. data:: IntType
Georg Brandla1e9ef72007-09-04 07:23:09 +000065 LongType
Georg Brandl116aa622007-08-15 14:28:22 +000066
67 The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
68
69
Georg Brandl116aa622007-08-15 14:28:22 +000070.. data:: FloatType
71
72 The type of floating point numbers (e.g. ``1.0``); alias of the built-in
73 :class:`float`.
74
75
76.. data:: ComplexType
77
Georg Brandla1e9ef72007-09-04 07:23:09 +000078 The type of complex numbers (e.g. ``1.0j``); alias of the built-in
79 :class:`complex`. This is not defined if Python was built without complex
80 number support.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
83.. data:: StringType
84
85 The type of character strings (e.g. ``'Spam'``); alias of the built-in
86 :class:`str`.
87
88
Georg Brandl116aa622007-08-15 14:28:22 +000089.. data:: TupleType
90
91 The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
92 :class:`tuple`.
93
94
95.. data:: ListType
96
97 The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
98 :class:`list`.
99
100
101.. data:: DictType
Georg Brandla1e9ef72007-09-04 07:23:09 +0000102 DictionaryType
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104 The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
105 built-in :class:`dict`.
106
107
Georg Brandl116aa622007-08-15 14:28:22 +0000108.. data:: FunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +0000109 LambdaType
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111 The type of user-defined functions and lambdas.
112
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114.. data:: GeneratorType
115
116 The type of generator-iterator objects, produced by calling a generator
117 function.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. data:: CodeType
121
122 .. index:: builtin: compile
123
124 The type for code objects such as returned by :func:`compile`.
125
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127.. data:: MethodType
Georg Brandla1e9ef72007-09-04 07:23:09 +0000128 UnboundMethdType
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 The type of methods of user-defined class instances.
131
132
Georg Brandl116aa622007-08-15 14:28:22 +0000133.. data:: BuiltinFunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +0000134 BuiltinMethodType
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136 The type of built-in functions like :func:`len` or :func:`sys.exit`.
137
138
Georg Brandl116aa622007-08-15 14:28:22 +0000139.. data:: ModuleType
140
141 The type of modules.
142
143
Georg Brandl116aa622007-08-15 14:28:22 +0000144.. data:: SliceType
145
146 .. index:: builtin: slice
147
148 The type of objects returned by :func:`slice`; alias of the built-in
149 :class:`slice`.
150
151
152.. data:: EllipsisType
153
154 The type of ``Ellipsis``.
155
156
157.. data:: TracebackType
158
159 The type of traceback objects such as found in ``sys.exc_info()[2]``.
160
161
162.. data:: FrameType
163
164 The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
165 traceback object.
166
167
Georg Brandla1e9ef72007-09-04 07:23:09 +0000168.. XXX!
Georg Brandl116aa622007-08-15 14:28:22 +0000169.. data:: BufferType
170
171 .. index:: builtin: buffer
172
173 The type of buffer objects created by the :func:`buffer` function.
174
175
176.. data:: DictProxyType
177
178 The type of dict proxies, such as ``TypeType.__dict__``.
179
180
181.. data:: NotImplementedType
182
183 The type of ``NotImplemented``
184
185
186.. data:: GetSetDescriptorType
187
188 The type of objects defined in extension modules with ``PyGetSetDef``, such as
189 ``FrameType.f_locals`` or ``array.array.typecode``. This constant is not
190 defined in implementations of Python that do not have such extension types, so
191 for portable code use ``hasattr(types, 'GetSetDescriptorType')``.
192
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194.. data:: MemberDescriptorType
195
196 The type of objects defined in extension modules with ``PyMemberDef``, such as
197 ``datetime.timedelta.days``. This constant is not defined in implementations of
198 Python that do not have such extension types, so for portable code use
199 ``hasattr(types, 'MemberDescriptorType')``.