PageRenderTime 373ms CodeModel.GetById 7ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Doc/library/types.rst

http://unladen-swallow.googlecode.com/
ReStructuredText | 258 lines | 138 code | 120 blank | 0 comment | 0 complexity | 6bee22828dfec5a0b52e65ab6a84815d MD5 | raw file
  1:mod:`types` --- Names for built-in types
  2=========================================
  3
  4.. module:: types
  5   :synopsis: Names for built-in types.
  6
  7
  8This module defines names for some object types that are used by the standard
  9Python interpreter, but not for the types defined by various extension modules.
 10Also, it does not include some of the types that arise during processing such as
 11the ``listiterator`` type. It is safe to use ``from types import *`` --- the
 12module does not export any names besides the ones listed here. New names
 13exported by future versions of this module will all end in ``Type``.
 14
 15Typical use is for functions that do different things depending on their
 16argument types, like the following::
 17
 18   from types import *
 19   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          LambdaType
120
121   The type of user-defined functions and functions created by :keyword:`lambda`
122   expressions.
123
124
125.. data:: GeneratorType
126
127   The type of :term:`generator`-iterator objects, produced by calling a
128   generator function.
129
130   .. versionadded:: 2.2
131
132
133.. data:: CodeType
134
135   .. index:: builtin: compile
136
137   The type for code objects such as returned by :func:`compile`.
138
139
140.. data:: ClassType
141
142   The type of user-defined old-style classes.
143
144
145.. data:: InstanceType
146
147   The type of instances of user-defined classes.
148
149
150.. data:: MethodType
151
152   The type of methods of user-defined class instances.
153
154
155.. data:: UnboundMethodType
156
157   An alternate name for ``MethodType``.
158
159
160.. data:: BuiltinFunctionType
161          BuiltinMethodType
162
163   The type of built-in functions like :func:`len` or :func:`sys.exit`, and
164   methods of built-in classes.  (Here, the term "built-in" means "written in
165   C".)
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:: XRangeType
180
181   .. index:: builtin: xrange
182
183   The type of range objects returned by :func:`xrange`; alias of the built-in
184   :class:`xrange`.
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_traceback``.
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
231   as ``FrameType.f_locals`` or ``array.array.typecode``.  This type is used as
232   descriptor for object attributes; it has the same purpose as the
233   :class:`property` type, but for classes defined in extension modules.
234
235   .. versionadded:: 2.5
236
237
238.. data:: MemberDescriptorType
239
240   The type of objects defined in extension modules with ``PyMemberDef``, such
241   as ``datetime.timedelta.days``.  This type is used as descriptor for simple C
242   data members which use standard conversion functions; it has the same purpose
243   as the :class:`property` type, but for classes defined in extension modules.
244   In other implementations of Python, this type may be identical to
245   ``GetSetDescriptorType``.
246
247   .. versionadded:: 2.5
248
249
250.. data:: StringTypes
251
252   A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate
253   easier checking for any string object.  Using this is more portable than using a
254   sequence of the two string types constructed elsewhere since it only contains
255   ``UnicodeType`` if it has been built in the running version of Python.  For
256   example: ``isinstance(s, types.StringTypes)``.
257
258   .. versionadded:: 2.2