summaryrefslogtreecommitdiffstats
path: root/Doc/library/types.rst
blob: 06832d14c6ed86495246b77a52ac635dcdd322e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

:mod:`types` --- Names for built-in types
=========================================

.. module:: types
   :synopsis: 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. It is safe to use ``from types import *`` --- the
module does not export any names besides the ones listed here. 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 *
   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 :func:`int` and
:func:`str` are also names for the corresponding types.  This is now the
preferred way to access the type instead of using the :mod:`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)

The module defines the following names:


.. data:: NoneType

   The type of ``None``.


.. data:: TypeType

   .. index:: builtin: type

   The type of type objects (such as returned by :func:`type`); alias of the
   built-in :class:`type`.


.. data:: BooleanType

   The type of the :class:`bool` values ``True`` and ``False``; alias of the
   built-in :class:`bool`.

   .. versionadded:: 2.3


.. data:: IntType

   The type of integers (e.g. ``1``); alias of the built-in :class:`int`.


.. data:: LongType

   The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.


.. data:: FloatType

   The type of floating point numbers (e.g. ``1.0``); alias of the built-in
   :class:`float`.


.. data:: ComplexType

   The type of complex numbers (e.g. ``1.0j``).  This is not defined if Python was
   built without complex number support.


.. data:: StringType

   The type of character strings (e.g. ``'Spam'``); alias of the built-in
   :class:`str`.


.. data:: UnicodeType

   The type of Unicode character strings (e.g. ``u'Spam'``).  This is not defined
   if Python was built without Unicode support.  It's an alias of the built-in
   :class:`unicode`.


.. data:: TupleType

   The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
   :class:`tuple`.


.. data:: ListType

   The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
   :class:`list`.


.. data:: DictType

   The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
   built-in :class:`dict`.


.. data:: DictionaryType

   An alternate name for ``DictType``.


.. data:: FunctionType

   The type of user-defined functions and lambdas.


.. data:: LambdaType

   An alternate name for ``FunctionType``.


.. data:: GeneratorType

   The type of generator-iterator objects, produced by calling a generator
   function.

   .. versionadded:: 2.2


.. data:: CodeType

   .. index:: builtin: compile

   The type for code objects such as returned by :func:`compile`.


.. data:: ClassType

   The type of user-defined classes.


.. data:: InstanceType

   The type of instances of user-defined classes.


.. data:: MethodType

   The type of methods of user-defined class instances.


.. data:: UnboundMethodType

   An alternate name for ``MethodType``.


.. data:: BuiltinFunctionType

   The type of built-in functions like :func:`len` or :func:`sys.exit`.


.. data:: BuiltinMethodType

   An alternate name for ``BuiltinFunction``.


.. data:: ModuleType

   The type of modules.


.. data:: FileType

   The type of open file objects such as ``sys.stdout``; alias of the built-in
   :class:`file`.


.. data:: XRangeType

   .. index:: builtin: xrange

   The type of range objects returned by :func:`xrange`; alias of the built-in
   :class:`xrange`.


.. data:: SliceType

   .. index:: builtin: slice

   The type of objects returned by :func:`slice`; alias of the built-in
   :class:`slice`.


.. data:: EllipsisType

   The type of ``Ellipsis``.


.. data:: TracebackType

   The type of traceback objects such as found in ``sys.exc_traceback``.


.. data:: FrameType

   The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
   traceback object.


.. data:: BufferType

   .. index:: builtin: buffer

   The type of buffer objects created by the :func:`buffer` function.


.. data:: DictProxyType

   The type of dict proxies, such as ``TypeType.__dict__``.


.. data:: NotImplementedType

   The type of ``NotImplemented``


.. data:: GetSetDescriptorType

   The type of objects defined in extension modules with ``PyGetSetDef``, such as
   ``FrameType.f_locals`` or ``array.array.typecode``.  This constant is not
   defined in implementations of Python that do not have such extension types, so
   for portable code use ``hasattr(types, 'GetSetDescriptorType')``.

   .. versionadded:: 2.5


.. data:: MemberDescriptorType

   The type of objects defined in extension modules with ``PyMemberDef``, such as
   ``datetime.timedelta.days``.  This constant is not defined in implementations of
   Python that do not have such extension types, so for portable code use
   ``hasattr(types, 'MemberDescriptorType')``.

   .. versionadded:: 2.5


.. data:: StringTypes

   A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate
   easier checking for any string object.  Using this is more portable than using a
   sequence of the two string types constructed elsewhere since it only contains
   ``UnicodeType`` if it has been built in the running version of Python.  For
   example: ``isinstance(s, types.StringTypes)``.

   .. versionadded:: 2.2