| 1 |
:mod:`types` --- Names for built-in types |
| 2 |
========================================= |
| 3 |
|
| 4 |
.. module:: types |
| 5 |
:synopsis: Names for built-in types. |
| 6 |
|
| 7 |
**Source code:** :source:`Lib/types.py` |
| 8 |
|
| 9 |
-------------- |
| 10 |
|
| 11 |
This module defines names for some object types that are used by the standard |
| 12 |
Python interpreter, but not for the types defined by various extension modules. |
| 13 |
Also, it does not include some of the types that arise during processing such as |
| 14 |
the ``listiterator`` type. It is safe to use ``from types import *`` --- the |
| 15 |
module does not export any names besides the ones listed here. New names |
| 16 |
exported by future versions of this module will all end in ``Type``. |
| 17 |
|
| 18 |
Typical use is for functions that do different things depending on their |
| 19 |
argument types, like the following:: |
| 20 |
|
| 21 |
from types import * |
| 22 |
def delete(mylist, item): |
| 23 |
if type(item) is IntType: |
| 24 |
del mylist[item] |
| 25 |
else: |
| 26 |
mylist.remove(item) |
| 27 |
|
| 28 |
Starting in Python 2.2, built-in factory functions such as :func:`int` and |
| 29 |
:func:`str` are also names for the corresponding types. This is now the |
| 30 |
preferred way to access the type instead of using the :mod:`types` module. |
| 31 |
Accordingly, the example above should be written as follows:: |
| 32 |
|
| 33 |
def delete(mylist, item): |
| 34 |
if isinstance(item, int): |
| 35 |
del mylist[item] |
| 36 |
else: |
| 37 |
mylist.remove(item) |
| 38 |
|
| 39 |
The module defines the following names: |
| 40 |
|
| 41 |
|
| 42 |
.. data:: NoneType |
| 43 |
|
| 44 |
The type of ``None``. |
| 45 |
|
| 46 |
|
| 47 |
.. data:: TypeType |
| 48 |
|
| 49 |
.. index:: builtin: type |
| 50 |
|
| 51 |
The type of type objects (such as returned by :func:`type`); alias of the |
| 52 |
built-in :class:`type`. |
| 53 |
|
| 54 |
|
| 55 |
.. data:: BooleanType |
| 56 |
|
| 57 |
The type of the :class:`bool` values ``True`` and ``False``; alias of the |
| 58 |
built-in :class:`bool`. |
| 59 |
|
| 60 |
.. versionadded:: 2.3 |
| 61 |
|
| 62 |
|
| 63 |
.. data:: IntType |
| 64 |
|
| 65 |
The type of integers (e.g. ``1``); alias of the built-in :class:`int`. |
| 66 |
|
| 67 |
|
| 68 |
.. data:: LongType |
| 69 |
|
| 70 |
The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`. |
| 71 |
|
| 72 |
|
| 73 |
.. data:: FloatType |
| 74 |
|
| 75 |
The type of floating point numbers (e.g. ``1.0``); alias of the built-in |
| 76 |
:class:`float`. |
| 77 |
|
| 78 |
|
| 79 |
.. data:: ComplexType |
| 80 |
|
| 81 |
The type of complex numbers (e.g. ``1.0j``). This is not defined if Python was |
| 82 |
built without complex number support. |
| 83 |
|
| 84 |
|
| 85 |
.. data:: StringType |
| 86 |
|
| 87 |
The type of character strings (e.g. ``'Spam'``); alias of the built-in |
| 88 |
:class:`str`. |
| 89 |
|
| 90 |
|
| 91 |
.. data:: UnicodeType |
| 92 |
|
| 93 |
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined |
| 94 |
if Python was built without Unicode support. It's an alias of the built-in |
| 95 |
:class:`unicode`. |
| 96 |
|
| 97 |
|
| 98 |
.. data:: TupleType |
| 99 |
|
| 100 |
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in |
| 101 |
:class:`tuple`. |
| 102 |
|
| 103 |
|
| 104 |
.. data:: ListType |
| 105 |
|
| 106 |
The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in |
| 107 |
:class:`list`. |
| 108 |
|
| 109 |
|
| 110 |
.. data:: DictType |
| 111 |
|
| 112 |
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the |
| 113 |
built-in :class:`dict`. |
| 114 |
|
| 115 |
|
| 116 |
.. data:: DictionaryType |
| 117 |
|
| 118 |
An alternate name for ``DictType``. |
| 119 |
|
| 120 |
|
| 121 |
.. data:: FunctionType |
| 122 |
LambdaType |
| 123 |
|
| 124 |
The type of user-defined functions and functions created by :keyword:`lambda` |
| 125 |
expressions. |
| 126 |
|
| 127 |
|
| 128 |
.. data:: GeneratorType |
| 129 |
|
| 130 |
The type of :term:`generator`-iterator objects, produced by calling a |
| 131 |
generator 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 old-style classes. |
| 146 |
|
| 147 |
|
| 148 |
.. data:: InstanceType |
| 149 |
|
| 150 |
The type of instances of user-defined old-style classes. |
| 151 |
|
| 152 |
|
| 153 |
.. data:: MethodType |
| 154 |
|
| 155 |
The type of methods of user-defined class instances. |
| 156 |
|
| 157 |
|
| 158 |
.. data:: UnboundMethodType |
| 159 |
|
| 160 |
An alternate name for ``MethodType``. |
| 161 |
|
| 162 |
|
| 163 |
.. data:: BuiltinFunctionType |
| 164 |
BuiltinMethodType |
| 165 |
|
| 166 |
The type of built-in functions like :func:`len` or :func:`sys.exit`, and |
| 167 |
methods of built-in classes. (Here, the term "built-in" means "written in |
| 168 |
C".) |
| 169 |
|
| 170 |
|
| 171 |
.. data:: ModuleType |
| 172 |
|
| 173 |
The type of modules. |
| 174 |
|
| 175 |
|
| 176 |
.. data:: FileType |
| 177 |
|
| 178 |
The type of open file objects such as ``sys.stdout``; alias of the built-in |
| 179 |
:class:`file`. |
| 180 |
|
| 181 |
|
| 182 |
.. data:: XRangeType |
| 183 |
|
| 184 |
.. index:: builtin: xrange |
| 185 |
|
| 186 |
The type of range objects returned by :func:`xrange`; alias of the built-in |
| 187 |
:class:`xrange`. |
| 188 |
|
| 189 |
|
| 190 |
.. data:: SliceType |
| 191 |
|
| 192 |
.. index:: builtin: slice |
| 193 |
|
| 194 |
The type of objects returned by :func:`slice`; alias of the built-in |
| 195 |
:class:`slice`. |
| 196 |
|
| 197 |
|
| 198 |
.. data:: EllipsisType |
| 199 |
|
| 200 |
The type of ``Ellipsis``. |
| 201 |
|
| 202 |
|
| 203 |
.. data:: TracebackType |
| 204 |
|
| 205 |
The type of traceback objects such as found in ``sys.exc_traceback``. |
| 206 |
|
| 207 |
|
| 208 |
.. data:: FrameType |
| 209 |
|
| 210 |
The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 211 |
traceback object. |
| 212 |
|
| 213 |
|
| 214 |
.. data:: BufferType |
| 215 |
|
| 216 |
.. index:: builtin: buffer |
| 217 |
|
| 218 |
The type of buffer objects created by the :func:`buffer` function. |
| 219 |
|
| 220 |
|
| 221 |
.. data:: DictProxyType |
| 222 |
|
| 223 |
The type of dict proxies, such as ``TypeType.__dict__``. |
| 224 |
|
| 225 |
|
| 226 |
.. data:: NotImplementedType |
| 227 |
|
| 228 |
The type of ``NotImplemented`` |
| 229 |
|
| 230 |
|
| 231 |
.. data:: GetSetDescriptorType |
| 232 |
|
| 233 |
The type of objects defined in extension modules with ``PyGetSetDef``, such |
| 234 |
as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as |
| 235 |
descriptor for object attributes; it has the same purpose as the |
| 236 |
:class:`property` type, but for classes defined in extension modules. |
| 237 |
|
| 238 |
.. versionadded:: 2.5 |
| 239 |
|
| 240 |
|
| 241 |
.. data:: MemberDescriptorType |
| 242 |
|
| 243 |
The type of objects defined in extension modules with ``PyMemberDef``, such |
| 244 |
as ``datetime.timedelta.days``. This type is used as descriptor for simple C |
| 245 |
data members which use standard conversion functions; it has the same purpose |
| 246 |
as the :class:`property` type, but for classes defined in extension modules. |
| 247 |
|
| 248 |
.. impl-detail:: |
| 249 |
|
| 250 |
In other implementations of Python, this type may be identical to |
| 251 |
``GetSetDescriptorType``. |
| 252 |
|
| 253 |
.. versionadded:: 2.5 |
| 254 |
|
| 255 |
|
| 256 |
.. data:: StringTypes |
| 257 |
|
| 258 |
A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate |
| 259 |
easier checking for any string object. Using this is more portable than using a |
| 260 |
sequence of the two string types constructed elsewhere since it only contains |
| 261 |
``UnicodeType`` if it has been built in the running version of Python. For |
| 262 |
example: ``isinstance(s, types.StringTypes)``. |
| 263 |
|
| 264 |
.. versionadded:: 2.2 |