blob: bd728d04f8bd06106f41c2d00739854f47d02338 [file] [log] [blame]
Nick Coghlan7fc570a2012-05-20 02:34:13 +10001:mod:`types` --- Dynamic type creation and names for built-in types
2===================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: types
5 :synopsis: Names for built-in types.
6
Raymond Hettingera1993682011-01-27 01:20:32 +00007**Source code:** :source:`Lib/types.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
Nick Coghlan7fc570a2012-05-20 02:34:13 +100011This module defines utility function to assist in dynamic creation of
12new types.
13
14It also defines names for some object types that are used by the standard
Georg Brandld66a0292008-08-23 15:14:57 +000015Python interpreter, but not exposed as builtins like :class:`int` or
Nick Coghlan7fc570a2012-05-20 02:34:13 +100016:class:`str` are.
Georg Brandl116aa622007-08-15 14:28:22 +000017
Christian Heimesc9543e42007-11-28 08:28:28 +000018
Nick Coghlan7fc570a2012-05-20 02:34:13 +100019Dynamic 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
56Standard Interpreter Types
57--------------------------
58
59This module provides names for many of the types that are required to
60implement a Python interpreter. It deliberately avoids including some of
61the types that arise only incidentally during processing such as the
62``listiterator`` type.
63
64Typical use is of these names is for :func:`isinstance` or
65:func:`issubclass` checks.
66
67Standard names are defined for the following types:
Georg Brandl116aa622007-08-15 14:28:22 +000068
Georg Brandl116aa622007-08-15 14:28:22 +000069.. data:: FunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000070 LambdaType
Georg Brandl116aa622007-08-15 14:28:22 +000071
Nick Coghlan7fc570a2012-05-20 02:34:13 +100072 The type of user-defined functions and functions created by
73 :keyword:`lambda` expressions.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
Georg Brandl116aa622007-08-15 14:28:22 +000076.. data:: GeneratorType
77
Georg Brandl9afde1c2007-11-01 20:32:30 +000078 The type of :term:`generator`-iterator objects, produced by calling a
79 generator function.
Georg Brandl116aa622007-08-15 14:28:22 +000080
Georg Brandl116aa622007-08-15 14:28:22 +000081
82.. data:: CodeType
83
84 .. index:: builtin: compile
85
86 The type for code objects such as returned by :func:`compile`.
87
88
Georg Brandl116aa622007-08-15 14:28:22 +000089.. data:: MethodType
90
91 The type of methods of user-defined class instances.
92
93
Georg Brandl116aa622007-08-15 14:28:22 +000094.. data:: BuiltinFunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000095 BuiltinMethodType
Georg Brandl116aa622007-08-15 14:28:22 +000096
Georg Brandld66a0292008-08-23 15:14:57 +000097 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 Brandl116aa622007-08-15 14:28:22 +0000100
101
Georg Brandl116aa622007-08-15 14:28:22 +0000102.. data:: ModuleType
103
104 The type of modules.
105
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107.. 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 Brandl116aa622007-08-15 14:28:22 +0000118.. data:: GetSetDescriptorType
119
Christian Heimes5e696852008-04-09 08:37:03 +0000120 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 Brandl116aa622007-08-15 14:28:22 +0000124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. data:: MemberDescriptorType
127
Christian Heimes5e696852008-04-09 08:37:03 +0000128 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 Brandl495f7b52009-10-27 15:28:25 +0000132
133 .. impl-detail::
134
135 In other implementations of Python, this type may be identical to
136 ``GetSetDescriptorType``.
Victor Stinner0db176f2012-04-16 00:16:30 +0200137
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