Issue #5262: Improved fix.
[python.git] / Doc / library / types.rst
blob87fa0d5e711e408b34308a4c946a3be6f084cce3
1 :mod:`types` --- Names for built-in types
2 =========================================
4 .. module:: types
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::
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)
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):
32           del mylist[item]
33        else:
34           mylist.remove(item)
36 The module defines the following names:
39 .. data:: NoneType
41    The type of ``None``.
44 .. data:: TypeType
46    .. index:: builtin: type
48    The type of type objects (such as returned by :func:`type`); alias of the
49    built-in :class:`type`.
52 .. data:: BooleanType
54    The type of the :class:`bool` values ``True`` and ``False``; alias of the
55    built-in :class:`bool`.
57    .. versionadded:: 2.3
60 .. data:: IntType
62    The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
65 .. data:: LongType
67    The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
70 .. data:: FloatType
72    The type of floating point numbers (e.g. ``1.0``); alias of the built-in
73    :class:`float`.
76 .. data:: ComplexType
78    The type of complex numbers (e.g. ``1.0j``).  This is not defined if Python was
79    built without complex number support.
82 .. data:: StringType
84    The type of character strings (e.g. ``'Spam'``); alias of the built-in
85    :class:`str`.
88 .. data:: UnicodeType
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`.
95 .. data:: TupleType
97    The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
98    :class:`tuple`.
101 .. data:: ListType
103    The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
104    :class:`list`.
107 .. data:: DictType
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
119           LambdaType
121    The type of user-defined functions and functions created by :keyword:`lambda`
122    expressions.
125 .. data:: GeneratorType
127    The type of :term:`generator`-iterator objects, produced by calling a
128    generator function.
130    .. versionadded:: 2.2
133 .. data:: CodeType
135    .. index:: builtin: compile
137    The type for code objects such as returned by :func:`compile`.
140 .. data:: ClassType
142    The type of user-defined old-style classes.
145 .. data:: InstanceType
147    The type of instances of user-defined classes.
150 .. data:: MethodType
152    The type of methods of user-defined class instances.
155 .. data:: UnboundMethodType
157    An alternate name for ``MethodType``.
160 .. data:: BuiltinFunctionType
161           BuiltinMethodType
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".)
168 .. data:: ModuleType
170    The type of modules.
173 .. data:: FileType
175    The type of open file objects such as ``sys.stdout``; alias of the built-in
176    :class:`file`.
179 .. data:: XRangeType
181    .. index:: builtin: xrange
183    The type of range objects returned by :func:`xrange`; alias of the built-in
184    :class:`xrange`.
187 .. data:: SliceType
189    .. index:: builtin: slice
191    The type of objects returned by :func:`slice`; alias of the built-in
192    :class:`slice`.
195 .. data:: EllipsisType
197    The type of ``Ellipsis``.
200 .. data:: TracebackType
202    The type of traceback objects such as found in ``sys.exc_traceback``.
205 .. data:: FrameType
207    The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
208    traceback object.
211 .. data:: BufferType
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.
244    In other implementations of Python, this type may be identical to
245    ``GetSetDescriptorType``.
247    .. versionadded:: 2.5
250 .. data:: StringTypes
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)``.
258    .. versionadded:: 2.2