Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`types` --- Names for built-in types |
| 3 | ========================================= |
| 4 | |
| 5 | .. module:: types |
| 6 | :synopsis: Names for built-in types. |
| 7 | |
| 8 | |
| 9 | This module defines names for some object types that are used by the standard |
| 10 | Python interpreter, but not for the types defined by various extension modules. |
| 11 | Also, it does not include some of the types that arise during processing such as |
Collin Winter | 2befd24 | 2007-08-28 06:09:03 +0000 | [diff] [blame] | 12 | the ``listiterator`` type. New names exported by future versions of this module |
| 13 | will all end in ``Type``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
| 15 | Typical use is for functions that do different things depending on their |
| 16 | argument types, like the following:: |
| 17 | |
Collin Winter | 2befd24 | 2007-08-28 06:09:03 +0000 | [diff] [blame] | 18 | from types import IntType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | def delete(mylist, item): |
| 20 | if type(item) is IntType: |
| 21 | del mylist[item] |
| 22 | else: |
| 23 | mylist.remove(item) |
| 24 | |
| 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:: |
| 29 | |
| 30 | def delete(mylist, item): |
| 31 | if isinstance(item, int): |
| 32 | del mylist[item] |
| 33 | else: |
| 34 | mylist.remove(item) |
| 35 | |
| 36 | The module defines the following names: |
| 37 | |
| 38 | |
| 39 | .. data:: NoneType |
| 40 | |
| 41 | The type of ``None``. |
| 42 | |
| 43 | |
| 44 | .. data:: TypeType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 45 | ClassType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
| 47 | .. index:: builtin: type |
| 48 | |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 49 | The type of type objects (such as returned by :func:`type`) and user-defined |
| 50 | classes without metaclass; alias of the built-in :class:`type`. |
| 51 | |
| 52 | |
| 53 | .. data:: ObjectType |
| 54 | |
| 55 | Alias of the built-in :func:`object`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | .. data:: BooleanType |
| 59 | |
| 60 | The type of the :class:`bool` values ``True`` and ``False``; alias of the |
| 61 | built-in :class:`bool`. |
| 62 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | .. data:: IntType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 65 | LongType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | The type of integers (e.g. ``1``); alias of the built-in :class:`int`. |
| 68 | |
| 69 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 78 | The type of complex numbers (e.g. ``1.0j``); alias of the built-in |
| 79 | :class:`complex`. This is not defined if Python was built without complex |
| 80 | number support. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | .. data:: StringType |
| 84 | |
| 85 | The type of character strings (e.g. ``'Spam'``); alias of the built-in |
| 86 | :class:`str`. |
| 87 | |
| 88 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | .. data:: TupleType |
| 90 | |
| 91 | The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in |
| 92 | :class:`tuple`. |
| 93 | |
| 94 | |
| 95 | .. data:: ListType |
| 96 | |
| 97 | The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in |
| 98 | :class:`list`. |
| 99 | |
| 100 | |
| 101 | .. data:: DictType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 102 | DictionaryType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 | |
| 104 | The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the |
| 105 | built-in :class:`dict`. |
| 106 | |
| 107 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | .. data:: FunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 109 | LambdaType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | |
| 111 | The type of user-defined functions and lambdas. |
| 112 | |
| 113 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | .. data:: GeneratorType |
| 115 | |
| 116 | The type of generator-iterator objects, produced by calling a generator |
| 117 | function. |
| 118 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
| 120 | .. data:: CodeType |
| 121 | |
| 122 | .. index:: builtin: compile |
| 123 | |
| 124 | The type for code objects such as returned by :func:`compile`. |
| 125 | |
| 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | .. data:: MethodType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 128 | UnboundMethdType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
| 130 | The type of methods of user-defined class instances. |
| 131 | |
| 132 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 134 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 136 | The type of built-in functions like :func:`len` or :func:`sys.exit`. |
| 137 | |
| 138 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | .. data:: ModuleType |
| 140 | |
| 141 | The type of modules. |
| 142 | |
| 143 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | .. data:: SliceType |
| 145 | |
| 146 | .. index:: builtin: slice |
| 147 | |
| 148 | The type of objects returned by :func:`slice`; alias of the built-in |
| 149 | :class:`slice`. |
| 150 | |
| 151 | |
| 152 | .. data:: EllipsisType |
| 153 | |
| 154 | The type of ``Ellipsis``. |
| 155 | |
| 156 | |
| 157 | .. data:: TracebackType |
| 158 | |
| 159 | The type of traceback objects such as found in ``sys.exc_info()[2]``. |
| 160 | |
| 161 | |
| 162 | .. data:: FrameType |
| 163 | |
| 164 | The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 165 | traceback object. |
| 166 | |
| 167 | |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame^] | 168 | .. XXX! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | .. data:: BufferType |
| 170 | |
| 171 | .. index:: builtin: buffer |
| 172 | |
| 173 | The type of buffer objects created by the :func:`buffer` function. |
| 174 | |
| 175 | |
| 176 | .. data:: DictProxyType |
| 177 | |
| 178 | The type of dict proxies, such as ``TypeType.__dict__``. |
| 179 | |
| 180 | |
| 181 | .. data:: NotImplementedType |
| 182 | |
| 183 | The type of ``NotImplemented`` |
| 184 | |
| 185 | |
| 186 | .. data:: GetSetDescriptorType |
| 187 | |
| 188 | The type of objects defined in extension modules with ``PyGetSetDef``, such as |
| 189 | ``FrameType.f_locals`` or ``array.array.typecode``. This constant is not |
| 190 | defined in implementations of Python that do not have such extension types, so |
| 191 | for portable code use ``hasattr(types, 'GetSetDescriptorType')``. |
| 192 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
| 194 | .. data:: MemberDescriptorType |
| 195 | |
| 196 | The type of objects defined in extension modules with ``PyMemberDef``, such as |
| 197 | ``datetime.timedelta.days``. This constant is not defined in implementations of |
| 198 | Python that do not have such extension types, so for portable code use |
| 199 | ``hasattr(types, 'MemberDescriptorType')``. |