Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 1 | :mod:`types` --- Dynamic type creation and names for built-in types |
| 2 | =================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: types |
| 5 | :synopsis: Names for built-in types. |
| 6 | |
Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/types.py` |
| 8 | |
| 9 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 11 | This module defines utility function to assist in dynamic creation of |
| 12 | new types. |
| 13 | |
| 14 | It also defines names for some object types that are used by the standard |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 15 | Python interpreter, but not exposed as builtins like :class:`int` or |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 16 | :class:`str` are. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
R David Murray | 13cc883 | 2014-02-25 16:03:14 -0500 | [diff] [blame] | 18 | Finally, it provides some additional type-related utility classes and functions |
| 19 | that are not fundamental enough to be builtins. |
| 20 | |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 21 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 22 | Dynamic Type Creation |
| 23 | --------------------- |
| 24 | |
| 25 | .. function:: new_class(name, bases=(), kwds=None, exec_body=None) |
| 26 | |
| 27 | Creates a class object dynamically using the appropriate metaclass. |
| 28 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 29 | The first three arguments are the components that make up a class |
| 30 | definition header: the class name, the base classes (in order), the |
| 31 | keyword arguments (such as ``metaclass``). |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 32 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 33 | The *exec_body* argument is a callback that is used to populate the |
| 34 | freshly created class namespace. It should accept the class namespace |
| 35 | as its sole argument and update the namespace directly with the class |
| 36 | contents. If no callback is provided, it has the same effect as passing |
| 37 | in ``lambda ns: ns``. |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 38 | |
Nick Coghlan | a497b44 | 2012-05-22 23:02:00 +1000 | [diff] [blame] | 39 | .. versionadded:: 3.3 |
| 40 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 41 | .. function:: prepare_class(name, bases=(), kwds=None) |
| 42 | |
| 43 | Calculates the appropriate metaclass and creates the class namespace. |
| 44 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 45 | The arguments are the components that make up a class definition header: |
| 46 | the class name, the base classes (in order) and the keyword arguments |
| 47 | (such as ``metaclass``). |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 48 | |
| 49 | The return value is a 3-tuple: ``metaclass, namespace, kwds`` |
| 50 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 51 | *metaclass* is the appropriate metaclass, *namespace* is the |
| 52 | prepared class namespace and *kwds* is an updated copy of the passed |
| 53 | in *kwds* argument with any ``'metaclass'`` entry removed. If no *kwds* |
| 54 | argument is passed in, this will be an empty dict. |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 55 | |
Nick Coghlan | a497b44 | 2012-05-22 23:02:00 +1000 | [diff] [blame] | 56 | .. versionadded:: 3.3 |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 57 | |
| 58 | .. seealso:: |
| 59 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 60 | :ref:`metaclasses` |
| 61 | Full details of the class creation process supported by these functions |
| 62 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 63 | :pep:`3115` - Metaclasses in Python 3000 |
| 64 | Introduced the ``__prepare__`` namespace hook |
| 65 | |
| 66 | |
| 67 | Standard Interpreter Types |
| 68 | -------------------------- |
| 69 | |
| 70 | This module provides names for many of the types that are required to |
| 71 | implement a Python interpreter. It deliberately avoids including some of |
| 72 | the types that arise only incidentally during processing such as the |
| 73 | ``listiterator`` type. |
| 74 | |
Ezio Melotti | d26c306 | 2012-08-26 07:33:10 +0300 | [diff] [blame] | 75 | Typical use of these names is for :func:`isinstance` or |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 76 | :func:`issubclass` checks. |
| 77 | |
| 78 | Standard names are defined for the following types: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | .. data:: FunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 81 | LambdaType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 83 | The type of user-defined functions and functions created by |
| 84 | :keyword:`lambda` expressions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | .. data:: GeneratorType |
| 88 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 89 | The type of :term:`generator`-iterator objects, produced by calling a |
| 90 | generator function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
| 93 | .. data:: CodeType |
| 94 | |
| 95 | .. index:: builtin: compile |
| 96 | |
| 97 | The type for code objects such as returned by :func:`compile`. |
| 98 | |
| 99 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | .. data:: MethodType |
| 101 | |
| 102 | The type of methods of user-defined class instances. |
| 103 | |
| 104 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 106 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 108 | The type of built-in functions like :func:`len` or :func:`sys.exit`, and |
| 109 | methods of built-in classes. (Here, the term "built-in" means "written in |
| 110 | C".) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
| 112 | |
Brett Cannon | 2d77204 | 2013-06-14 19:19:57 -0400 | [diff] [blame] | 113 | .. class:: ModuleType(name, doc=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
Brett Cannon | 2d77204 | 2013-06-14 19:19:57 -0400 | [diff] [blame] | 115 | The type of :term:`modules <module>`. Constructor takes the name of the |
| 116 | module to be created and optionally its :term:`docstring`. |
| 117 | |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 118 | .. note:: |
| 119 | Use :func:`importlib.util.module_from_spec` to create a new module if you |
| 120 | wish to set the various import-controlled attributes. |
| 121 | |
Brett Cannon | 2d77204 | 2013-06-14 19:19:57 -0400 | [diff] [blame] | 122 | .. attribute:: __doc__ |
| 123 | |
| 124 | The :term:`docstring` of the module. Defaults to ``None``. |
| 125 | |
| 126 | .. attribute:: __loader__ |
| 127 | |
| 128 | The :term:`loader` which loaded the module. Defaults to ``None``. |
| 129 | |
| 130 | .. versionchanged:: 3.4 |
| 131 | Defaults to ``None``. Previously the attribute was optional. |
| 132 | |
| 133 | .. attribute:: __name__ |
| 134 | |
| 135 | The name of the module. |
| 136 | |
| 137 | .. attribute:: __package__ |
| 138 | |
| 139 | Which :term:`package` a module belongs to. If the module is top-level |
| 140 | (i.e. not a part of any specific package) then the attribute should be set |
| 141 | to ``''``, else it should be set to the name of the package (which can be |
| 142 | :attr:`__name__` if the module is a package itself). Defaults to ``None``. |
| 143 | |
| 144 | .. versionchanged:: 3.4 |
| 145 | Defaults to ``None``. Previously the attribute was optional. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
| 147 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | .. data:: TracebackType |
| 149 | |
| 150 | The type of traceback objects such as found in ``sys.exc_info()[2]``. |
| 151 | |
| 152 | |
| 153 | .. data:: FrameType |
| 154 | |
| 155 | The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 156 | traceback object. |
| 157 | |
| 158 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | .. data:: GetSetDescriptorType |
| 160 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 161 | The type of objects defined in extension modules with ``PyGetSetDef``, such |
| 162 | as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as |
| 163 | descriptor for object attributes; it has the same purpose as the |
| 164 | :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | |
| 167 | .. data:: MemberDescriptorType |
| 168 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 169 | The type of objects defined in extension modules with ``PyMemberDef``, such |
| 170 | as ``datetime.timedelta.days``. This type is used as descriptor for simple C |
| 171 | data members which use standard conversion functions; it has the same purpose |
| 172 | as the :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 173 | |
| 174 | .. impl-detail:: |
| 175 | |
| 176 | In other implementations of Python, this type may be identical to |
| 177 | ``GetSetDescriptorType``. |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 178 | |
| 179 | .. class:: MappingProxyType(mapping) |
| 180 | |
| 181 | Read-only proxy of a mapping. It provides a dynamic view on the mapping's |
| 182 | entries, which means that when the mapping changes, the view reflects these |
| 183 | changes. |
| 184 | |
| 185 | .. versionadded:: 3.3 |
| 186 | |
| 187 | .. describe:: key in proxy |
| 188 | |
| 189 | Return ``True`` if the underlying mapping has a key *key*, else |
| 190 | ``False``. |
| 191 | |
| 192 | .. describe:: proxy[key] |
| 193 | |
| 194 | Return the item of the underlying mapping with key *key*. Raises a |
| 195 | :exc:`KeyError` if *key* is not in the underlying mapping. |
| 196 | |
| 197 | .. describe:: iter(proxy) |
| 198 | |
| 199 | Return an iterator over the keys of the underlying mapping. This is a |
| 200 | shortcut for ``iter(proxy.keys())``. |
| 201 | |
| 202 | .. describe:: len(proxy) |
| 203 | |
| 204 | Return the number of items in the underlying mapping. |
| 205 | |
| 206 | .. method:: copy() |
| 207 | |
| 208 | Return a shallow copy of the underlying mapping. |
| 209 | |
| 210 | .. method:: get(key[, default]) |
| 211 | |
| 212 | Return the value for *key* if *key* is in the underlying mapping, else |
| 213 | *default*. If *default* is not given, it defaults to ``None``, so that |
| 214 | this method never raises a :exc:`KeyError`. |
| 215 | |
| 216 | .. method:: items() |
| 217 | |
| 218 | Return a new view of the underlying mapping's items (``(key, value)`` |
| 219 | pairs). |
| 220 | |
| 221 | .. method:: keys() |
| 222 | |
| 223 | Return a new view of the underlying mapping's keys. |
| 224 | |
| 225 | .. method:: values() |
| 226 | |
| 227 | Return a new view of the underlying mapping's values. |
| 228 | |
| 229 | |
R David Murray | 13cc883 | 2014-02-25 16:03:14 -0500 | [diff] [blame] | 230 | Additional Utility Classes and Functions |
| 231 | ---------------------------------------- |
| 232 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 233 | .. class:: SimpleNamespace |
| 234 | |
| 235 | A simple :class:`object` subclass that provides attribute access to its |
| 236 | namespace, as well as a meaningful repr. |
| 237 | |
| 238 | Unlike :class:`object`, with ``SimpleNamespace`` you can add and remove |
| 239 | attributes. If a ``SimpleNamespace`` object is initialized with keyword |
| 240 | arguments, those are directly added to the underlying namespace. |
| 241 | |
| 242 | The type is roughly equivalent to the following code:: |
| 243 | |
| 244 | class SimpleNamespace: |
| 245 | def __init__(self, **kwargs): |
| 246 | self.__dict__.update(kwargs) |
| 247 | def __repr__(self): |
| 248 | keys = sorted(self.__dict__) |
| 249 | items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) |
| 250 | return "{}({})".format(type(self).__name__, ", ".join(items)) |
Eric Snow | b5c8f92 | 2013-02-16 16:32:39 -0700 | [diff] [blame] | 251 | def __eq__(self, other): |
| 252 | return self.__dict__ == other.__dict__ |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 253 | |
| 254 | ``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``. |
| 255 | However, for a structured record type use :func:`~collections.namedtuple` |
| 256 | instead. |
| 257 | |
| 258 | .. versionadded:: 3.3 |
R David Murray | 13cc883 | 2014-02-25 16:03:14 -0500 | [diff] [blame] | 259 | |
| 260 | |
| 261 | .. function:: DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None) |
| 262 | |
| 263 | Route attribute access on a class to __getattr__. |
| 264 | |
| 265 | This is a descriptor, used to define attributes that act differently when |
| 266 | accessed through an instance and through a class. Instance access remains |
| 267 | normal, but access to an attribute through a class will be routed to the |
| 268 | class's __getattr__ method; this is done by raising AttributeError. |
| 269 | |
| 270 | This allows one to have properties active on an instance, and have virtual |
| 271 | attributes on the class with the same name (see Enum for an example). |
| 272 | |
| 273 | .. versionadded:: 3.4 |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 274 | |
| 275 | |
| 276 | Coroutines Utility Functions |
| 277 | ---------------------------- |
| 278 | |
| 279 | .. function:: coroutine(gen_func) |
| 280 | |
| 281 | The function transforms a generator function to a :term:`coroutine function`, |
| 282 | so that it returns a :term:`coroutine` object. |
| 283 | |
| 284 | *gen_func* is modified in-place, hence the function can be used as a |
| 285 | decorator. |
| 286 | |
| 287 | .. versionadded:: 3.5 |