blob: 6d499fb6d8109629487c076bc8b37550c07dd47e [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
45
46 .. index:: builtin: type
47
48 The type of type objects (such as returned by :func:`type`); alias of the
49 built-in :class:`type`.
50
51
52.. data:: BooleanType
53
54 The type of the :class:`bool` values ``True`` and ``False``; alias of the
55 built-in :class:`bool`.
56
Georg Brandl116aa622007-08-15 14:28:22 +000057
58.. data:: IntType
59
60 The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
61
62
63.. data:: LongType
64
65 The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
66
67
68.. data:: FloatType
69
70 The type of floating point numbers (e.g. ``1.0``); alias of the built-in
71 :class:`float`.
72
73
74.. data:: ComplexType
75
76 The type of complex numbers (e.g. ``1.0j``). This is not defined if Python was
77 built without complex number support.
78
79
80.. data:: StringType
81
82 The type of character strings (e.g. ``'Spam'``); alias of the built-in
83 :class:`str`.
84
85
Georg Brandl116aa622007-08-15 14:28:22 +000086.. data:: TupleType
87
88 The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
89 :class:`tuple`.
90
91
92.. data:: ListType
93
94 The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
95 :class:`list`.
96
97
98.. data:: DictType
99
100 The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
101 built-in :class:`dict`.
102
103
104.. data:: DictionaryType
105
106 An alternate name for ``DictType``.
107
108
109.. data:: FunctionType
110
111 The type of user-defined functions and lambdas.
112
113
114.. data:: LambdaType
115
116 An alternate name for ``FunctionType``.
117
118
119.. data:: GeneratorType
120
121 The type of generator-iterator objects, produced by calling a generator
122 function.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125.. data:: CodeType
126
127 .. index:: builtin: compile
128
129 The type for code objects such as returned by :func:`compile`.
130
131
132.. data:: ClassType
133
134 The type of user-defined classes.
135
136
137.. data:: MethodType
138
139 The type of methods of user-defined class instances.
140
141
142.. data:: UnboundMethodType
143
144 An alternate name for ``MethodType``.
145
146
147.. data:: BuiltinFunctionType
148
149 The type of built-in functions like :func:`len` or :func:`sys.exit`.
150
151
152.. data:: BuiltinMethodType
153
154 An alternate name for ``BuiltinFunction``.
155
156
157.. data:: ModuleType
158
159 The type of modules.
160
161
162.. data:: FileType
163
164 The type of open file objects such as ``sys.stdout``; alias of the built-in
165 :class:`file`.
166
167
168.. data:: RangeType
169
170 .. index:: builtin: range
171
172 The type of range objects returned by :func:`range`; alias of the built-in
173 :class:`range`.
174
175
176.. data:: SliceType
177
178 .. index:: builtin: slice
179
180 The type of objects returned by :func:`slice`; alias of the built-in
181 :class:`slice`.
182
183
184.. data:: EllipsisType
185
186 The type of ``Ellipsis``.
187
188
189.. data:: TracebackType
190
191 The type of traceback objects such as found in ``sys.exc_info()[2]``.
192
193
194.. data:: FrameType
195
196 The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
197 traceback object.
198
199
200.. data:: BufferType
201
202 .. index:: builtin: buffer
203
204 The type of buffer objects created by the :func:`buffer` function.
205
206
207.. data:: DictProxyType
208
209 The type of dict proxies, such as ``TypeType.__dict__``.
210
211
212.. data:: NotImplementedType
213
214 The type of ``NotImplemented``
215
216
217.. data:: GetSetDescriptorType
218
219 The type of objects defined in extension modules with ``PyGetSetDef``, such as
220 ``FrameType.f_locals`` or ``array.array.typecode``. This constant is not
221 defined in implementations of Python that do not have such extension types, so
222 for portable code use ``hasattr(types, 'GetSetDescriptorType')``.
223
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225.. data:: MemberDescriptorType
226
227 The type of objects defined in extension modules with ``PyMemberDef``, such as
228 ``datetime.timedelta.days``. This constant is not defined in implementations of
229 Python that do not have such extension types, so for portable code use
230 ``hasattr(types, 'MemberDescriptorType')``.
231
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233.. data:: StringTypes
234
235 A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate
236 easier checking for any string object. Using this is more portable than using a
237 sequence of the two string types constructed elsewhere since it only contains
238 ``UnicodeType`` if it has been built in the running version of Python. For
239 example: ``isinstance(s, types.StringTypes)``.