1 :mod:`types` --- Names for built-in types
2 =========================================
5 :synopsis: Names for built-in types.
8 This module defines names for some object types that are used by the standard
9 Python interpreter, but not for the types defined by various extension modules.
10 Also, it does not include some of the types that arise during processing such as
11 the ``listiterator`` type. It is safe to use ``from types import *`` --- the
12 module does not export any names besides the ones listed here. New names
13 exported by future versions of this module will all end in ``Type``.
15 Typical use is for functions that do different things depending on their
16 argument types, like the following::
19 def delete(mylist, item):
20 if type(item) is IntType:
25 Starting 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
27 preferred way to access the type instead of using the :mod:`types` module.
28 Accordingly, the example above should be written as follows::
30 def delete(mylist, item):
31 if isinstance(item, int):
36 The module defines the following names:
46 .. index:: builtin: type
48 The type of type objects (such as returned by :func:`type`); alias of the
49 built-in :class:`type`.
54 The type of the :class:`bool` values ``True`` and ``False``; alias of the
55 built-in :class:`bool`.
62 The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
67 The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
72 The type of floating point numbers (e.g. ``1.0``); alias of the built-in
78 The type of complex numbers (e.g. ``1.0j``). This is not defined if Python was
79 built without complex number support.
84 The type of character strings (e.g. ``'Spam'``); alias of the built-in
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
97 The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
103 The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
109 The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
110 built-in :class:`dict`.
113 .. data:: DictionaryType
115 An alternate name for ``DictType``.
118 .. data:: FunctionType
121 The type of user-defined functions and functions created by :keyword:`lambda`
125 .. data:: GeneratorType
127 The type of :term:`generator`-iterator objects, produced by calling a
130 .. versionadded:: 2.2
135 .. index:: builtin: compile
137 The type for code objects such as returned by :func:`compile`.
142 The type of user-defined old-style classes.
145 .. data:: InstanceType
147 The type of instances of user-defined classes.
152 The type of methods of user-defined class instances.
155 .. data:: UnboundMethodType
157 An alternate name for ``MethodType``.
160 .. data:: BuiltinFunctionType
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
175 The type of open file objects such as ``sys.stdout``; alias of the built-in
181 .. index:: builtin: xrange
183 The type of range objects returned by :func:`xrange`; alias of the built-in
189 .. index:: builtin: slice
191 The type of objects returned by :func:`slice`; alias of the built-in
195 .. data:: EllipsisType
197 The type of ``Ellipsis``.
200 .. data:: TracebackType
202 The type of traceback objects such as found in ``sys.exc_traceback``.
207 The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
213 .. index:: builtin: buffer
215 The type of buffer objects created by the :func:`buffer` function.
218 .. data:: DictProxyType
220 The type of dict proxies, such as ``TypeType.__dict__``.
223 .. data:: NotImplementedType
225 The type of ``NotImplemented``
228 .. data:: GetSetDescriptorType
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.
235 .. versionadded:: 2.5
238 .. data:: MemberDescriptorType
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.
247 In other implementations of Python, this type may be identical to
248 ``GetSetDescriptorType``.
250 .. versionadded:: 2.5
253 .. data:: StringTypes
255 A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate
256 easier checking for any string object. Using this is more portable than using a
257 sequence of the two string types constructed elsewhere since it only contains
258 ``UnicodeType`` if it has been built in the running version of Python. For
259 example: ``isinstance(s, types.StringTypes)``.
261 .. versionadded:: 2.2