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 | |
| 26 | The arguments are the components that make up a class definition: the |
| 27 | class name, the base classes (in order), the keyword arguments (such as |
| 28 | ``metaclass``) and the callback function to populate the class namespace. |
| 29 | |
| 30 | The *exec_body* callback should accept the class namespace as its sole |
| 31 | argument and update the namespace directly with the class contents. |
| 32 | |
| 33 | .. function:: prepare_class(name, bases=(), kwds=None) |
| 34 | |
| 35 | Calculates the appropriate metaclass and creates the class namespace. |
| 36 | |
| 37 | The arguments are the components that make up a class definition: the |
| 38 | class name, the base classes (in order) and the keyword arguments (such as |
| 39 | ``metaclass``). |
| 40 | |
| 41 | The return value is a 3-tuple: ``metaclass, namespace, kwds`` |
| 42 | |
| 43 | *metaclass* is the appropriate metaclass |
| 44 | *namespace* is the prepared class namespace |
| 45 | *kwds* is an updated copy of the passed in *kwds* argument with any |
| 46 | ``'metaclass'`` entry removed. If no *kwds* argument is passed in, this |
| 47 | will be an empty dict. |
| 48 | |
| 49 | |
| 50 | .. seealso:: |
| 51 | |
| 52 | :pep:`3115` - Metaclasses in Python 3000 |
| 53 | Introduced the ``__prepare__`` namespace hook |
| 54 | |
| 55 | |
| 56 | Standard Interpreter Types |
| 57 | -------------------------- |
| 58 | |
| 59 | This module provides names for many of the types that are required to |
| 60 | implement a Python interpreter. It deliberately avoids including some of |
| 61 | the types that arise only incidentally during processing such as the |
| 62 | ``listiterator`` type. |
| 63 | |
| 64 | Typical use is of these names is for :func:`isinstance` or |
| 65 | :func:`issubclass` checks. |
| 66 | |
| 67 | Standard names are defined for the following types: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | .. data:: FunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 70 | LambdaType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame^] | 72 | The type of user-defined functions and functions created by |
| 73 | :keyword:`lambda` expressions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | .. data:: GeneratorType |
| 77 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 78 | The type of :term:`generator`-iterator objects, produced by calling a |
| 79 | generator function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | .. data:: CodeType |
| 83 | |
| 84 | .. index:: builtin: compile |
| 85 | |
| 86 | The type for code objects such as returned by :func:`compile`. |
| 87 | |
| 88 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | .. data:: MethodType |
| 90 | |
| 91 | The type of methods of user-defined class instances. |
| 92 | |
| 93 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 95 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 97 | The type of built-in functions like :func:`len` or :func:`sys.exit`, and |
| 98 | methods of built-in classes. (Here, the term "built-in" means "written in |
| 99 | C".) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | .. data:: ModuleType |
| 103 | |
| 104 | The type of modules. |
| 105 | |
| 106 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | .. data:: TracebackType |
| 108 | |
| 109 | The type of traceback objects such as found in ``sys.exc_info()[2]``. |
| 110 | |
| 111 | |
| 112 | .. data:: FrameType |
| 113 | |
| 114 | The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 115 | traceback object. |
| 116 | |
| 117 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | .. data:: GetSetDescriptorType |
| 119 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 120 | The type of objects defined in extension modules with ``PyGetSetDef``, such |
| 121 | as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as |
| 122 | descriptor for object attributes; it has the same purpose as the |
| 123 | :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 126 | .. data:: MemberDescriptorType |
| 127 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 128 | The type of objects defined in extension modules with ``PyMemberDef``, such |
| 129 | as ``datetime.timedelta.days``. This type is used as descriptor for simple C |
| 130 | data members which use standard conversion functions; it has the same purpose |
| 131 | as the :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 132 | |
| 133 | .. impl-detail:: |
| 134 | |
| 135 | In other implementations of Python, this type may be identical to |
| 136 | ``GetSetDescriptorType``. |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 137 | |
| 138 | .. class:: MappingProxyType(mapping) |
| 139 | |
| 140 | Read-only proxy of a mapping. It provides a dynamic view on the mapping's |
| 141 | entries, which means that when the mapping changes, the view reflects these |
| 142 | changes. |
| 143 | |
| 144 | .. versionadded:: 3.3 |
| 145 | |
| 146 | .. describe:: key in proxy |
| 147 | |
| 148 | Return ``True`` if the underlying mapping has a key *key*, else |
| 149 | ``False``. |
| 150 | |
| 151 | .. describe:: proxy[key] |
| 152 | |
| 153 | Return the item of the underlying mapping with key *key*. Raises a |
| 154 | :exc:`KeyError` if *key* is not in the underlying mapping. |
| 155 | |
| 156 | .. describe:: iter(proxy) |
| 157 | |
| 158 | Return an iterator over the keys of the underlying mapping. This is a |
| 159 | shortcut for ``iter(proxy.keys())``. |
| 160 | |
| 161 | .. describe:: len(proxy) |
| 162 | |
| 163 | Return the number of items in the underlying mapping. |
| 164 | |
| 165 | .. method:: copy() |
| 166 | |
| 167 | Return a shallow copy of the underlying mapping. |
| 168 | |
| 169 | .. method:: get(key[, default]) |
| 170 | |
| 171 | Return the value for *key* if *key* is in the underlying mapping, else |
| 172 | *default*. If *default* is not given, it defaults to ``None``, so that |
| 173 | this method never raises a :exc:`KeyError`. |
| 174 | |
| 175 | .. method:: items() |
| 176 | |
| 177 | Return a new view of the underlying mapping's items (``(key, value)`` |
| 178 | pairs). |
| 179 | |
| 180 | .. method:: keys() |
| 181 | |
| 182 | Return a new view of the underlying mapping's keys. |
| 183 | |
| 184 | .. method:: values() |
| 185 | |
| 186 | Return a new view of the underlying mapping's values. |
| 187 | |
| 188 | |