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 | |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 18 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 19 | Dynamic Type Creation |
| 20 | --------------------- |
| 21 | |
| 22 | .. function:: new_class(name, bases=(), kwds=None, exec_body=None) |
| 23 | |
| 24 | Creates a class object dynamically using the appropriate metaclass. |
| 25 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 26 | The first three arguments are the components that make up a class |
| 27 | definition header: the class name, the base classes (in order), the |
| 28 | keyword arguments (such as ``metaclass``). |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 29 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 30 | The *exec_body* argument is a callback that is used to populate the |
| 31 | freshly created class namespace. It should accept the class namespace |
| 32 | as its sole argument and update the namespace directly with the class |
| 33 | contents. If no callback is provided, it has the same effect as passing |
| 34 | in ``lambda ns: ns``. |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 35 | |
Nick Coghlan | a497b44 | 2012-05-22 23:02:00 +1000 | [diff] [blame] | 36 | .. versionadded:: 3.3 |
| 37 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 38 | .. function:: prepare_class(name, bases=(), kwds=None) |
| 39 | |
| 40 | Calculates the appropriate metaclass and creates the class namespace. |
| 41 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 42 | The arguments are the components that make up a class definition header: |
| 43 | the class name, the base classes (in order) and the keyword arguments |
| 44 | (such as ``metaclass``). |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 45 | |
| 46 | The return value is a 3-tuple: ``metaclass, namespace, kwds`` |
| 47 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 48 | *metaclass* is the appropriate metaclass, *namespace* is the |
| 49 | prepared class namespace and *kwds* is an updated copy of the passed |
| 50 | in *kwds* argument with any ``'metaclass'`` entry removed. If no *kwds* |
| 51 | argument is passed in, this will be an empty dict. |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 52 | |
Nick Coghlan | a497b44 | 2012-05-22 23:02:00 +1000 | [diff] [blame] | 53 | .. versionadded:: 3.3 |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 54 | |
| 55 | .. seealso:: |
| 56 | |
Nick Coghlan | a0cf90e | 2012-05-30 22:17:30 +1000 | [diff] [blame] | 57 | :ref:`metaclasses` |
| 58 | Full details of the class creation process supported by these functions |
| 59 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 60 | :pep:`3115` - Metaclasses in Python 3000 |
| 61 | Introduced the ``__prepare__`` namespace hook |
| 62 | |
| 63 | |
| 64 | Standard Interpreter Types |
| 65 | -------------------------- |
| 66 | |
| 67 | This module provides names for many of the types that are required to |
| 68 | implement a Python interpreter. It deliberately avoids including some of |
| 69 | the types that arise only incidentally during processing such as the |
| 70 | ``listiterator`` type. |
| 71 | |
Ezio Melotti | d26c306 | 2012-08-26 07:33:10 +0300 | [diff] [blame] | 72 | Typical use of these names is for :func:`isinstance` or |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 73 | :func:`issubclass` checks. |
| 74 | |
| 75 | Standard names are defined for the following types: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | .. data:: FunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 78 | LambdaType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 80 | The type of user-defined functions and functions created by |
| 81 | :keyword:`lambda` expressions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
| 83 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | .. data:: GeneratorType |
| 85 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 86 | The type of :term:`generator`-iterator objects, produced by calling a |
| 87 | generator function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | |
| 90 | .. data:: CodeType |
| 91 | |
| 92 | .. index:: builtin: compile |
| 93 | |
| 94 | The type for code objects such as returned by :func:`compile`. |
| 95 | |
| 96 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | .. data:: MethodType |
| 98 | |
| 99 | The type of methods of user-defined class instances. |
| 100 | |
| 101 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 103 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 105 | The type of built-in functions like :func:`len` or :func:`sys.exit`, and |
| 106 | methods of built-in classes. (Here, the term "built-in" means "written in |
| 107 | C".) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
| 109 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | .. data:: ModuleType |
| 111 | |
| 112 | The type of modules. |
| 113 | |
| 114 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | .. data:: TracebackType |
| 116 | |
| 117 | The type of traceback objects such as found in ``sys.exc_info()[2]``. |
| 118 | |
| 119 | |
| 120 | .. data:: FrameType |
| 121 | |
| 122 | The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 123 | traceback object. |
| 124 | |
| 125 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | .. data:: GetSetDescriptorType |
| 127 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 128 | The type of objects defined in extension modules with ``PyGetSetDef``, such |
| 129 | as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as |
| 130 | descriptor for object attributes; it has the same purpose as the |
| 131 | :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | |
| 134 | .. data:: MemberDescriptorType |
| 135 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 136 | The type of objects defined in extension modules with ``PyMemberDef``, such |
| 137 | as ``datetime.timedelta.days``. This type is used as descriptor for simple C |
| 138 | data members which use standard conversion functions; it has the same purpose |
| 139 | as the :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 140 | |
| 141 | .. impl-detail:: |
| 142 | |
| 143 | In other implementations of Python, this type may be identical to |
| 144 | ``GetSetDescriptorType``. |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 145 | |
| 146 | .. class:: MappingProxyType(mapping) |
| 147 | |
| 148 | Read-only proxy of a mapping. It provides a dynamic view on the mapping's |
| 149 | entries, which means that when the mapping changes, the view reflects these |
| 150 | changes. |
| 151 | |
| 152 | .. versionadded:: 3.3 |
| 153 | |
| 154 | .. describe:: key in proxy |
| 155 | |
| 156 | Return ``True`` if the underlying mapping has a key *key*, else |
| 157 | ``False``. |
| 158 | |
| 159 | .. describe:: proxy[key] |
| 160 | |
| 161 | Return the item of the underlying mapping with key *key*. Raises a |
| 162 | :exc:`KeyError` if *key* is not in the underlying mapping. |
| 163 | |
| 164 | .. describe:: iter(proxy) |
| 165 | |
| 166 | Return an iterator over the keys of the underlying mapping. This is a |
| 167 | shortcut for ``iter(proxy.keys())``. |
| 168 | |
| 169 | .. describe:: len(proxy) |
| 170 | |
| 171 | Return the number of items in the underlying mapping. |
| 172 | |
| 173 | .. method:: copy() |
| 174 | |
| 175 | Return a shallow copy of the underlying mapping. |
| 176 | |
| 177 | .. method:: get(key[, default]) |
| 178 | |
| 179 | Return the value for *key* if *key* is in the underlying mapping, else |
| 180 | *default*. If *default* is not given, it defaults to ``None``, so that |
| 181 | this method never raises a :exc:`KeyError`. |
| 182 | |
| 183 | .. method:: items() |
| 184 | |
| 185 | Return a new view of the underlying mapping's items (``(key, value)`` |
| 186 | pairs). |
| 187 | |
| 188 | .. method:: keys() |
| 189 | |
| 190 | Return a new view of the underlying mapping's keys. |
| 191 | |
| 192 | .. method:: values() |
| 193 | |
| 194 | Return a new view of the underlying mapping's values. |
| 195 | |
| 196 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 197 | .. class:: SimpleNamespace |
| 198 | |
| 199 | A simple :class:`object` subclass that provides attribute access to its |
| 200 | namespace, as well as a meaningful repr. |
| 201 | |
| 202 | Unlike :class:`object`, with ``SimpleNamespace`` you can add and remove |
| 203 | attributes. If a ``SimpleNamespace`` object is initialized with keyword |
| 204 | arguments, those are directly added to the underlying namespace. |
| 205 | |
| 206 | The type is roughly equivalent to the following code:: |
| 207 | |
| 208 | class SimpleNamespace: |
| 209 | def __init__(self, **kwargs): |
| 210 | self.__dict__.update(kwargs) |
| 211 | def __repr__(self): |
| 212 | keys = sorted(self.__dict__) |
| 213 | items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) |
| 214 | return "{}({})".format(type(self).__name__, ", ".join(items)) |
| 215 | |
| 216 | ``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``. |
| 217 | However, for a structured record type use :func:`~collections.namedtuple` |
| 218 | instead. |
| 219 | |
| 220 | .. versionadded:: 3.3 |