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