blob: 0368177371babf5053648e4936fc844f496a25c0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`types` --- Names for built-in types
2=========================================
3
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
11This module defines names for some object types that are used by the standard
Georg Brandld66a0292008-08-23 15:14:57 +000012Python interpreter, but not exposed as builtins like :class:`int` or
13:class:`str` are. Also, it does not include some of the types that arise
14transparently during processing such as the ``listiterator`` type.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Georg Brandld66a0292008-08-23 15:14:57 +000016Typical use is for :func:`isinstance` or :func:`issubclass` checks.
Christian Heimesc9543e42007-11-28 08:28:28 +000017
Georg Brandl116aa622007-08-15 14:28:22 +000018The module defines the following names:
19
Georg Brandl116aa622007-08-15 14:28:22 +000020.. data:: FunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000021 LambdaType
Georg Brandl116aa622007-08-15 14:28:22 +000022
Georg Brandld66a0292008-08-23 15:14:57 +000023 The type of user-defined functions and functions created by :keyword:`lambda`
24 expressions.
Georg Brandl116aa622007-08-15 14:28:22 +000025
26
Georg Brandl116aa622007-08-15 14:28:22 +000027.. data:: GeneratorType
28
Georg Brandl9afde1c2007-11-01 20:32:30 +000029 The type of :term:`generator`-iterator objects, produced by calling a
30 generator function.
Georg Brandl116aa622007-08-15 14:28:22 +000031
Georg Brandl116aa622007-08-15 14:28:22 +000032
33.. data:: CodeType
34
35 .. index:: builtin: compile
36
37 The type for code objects such as returned by :func:`compile`.
38
39
Georg Brandl116aa622007-08-15 14:28:22 +000040.. data:: MethodType
41
42 The type of methods of user-defined class instances.
43
44
Georg Brandl116aa622007-08-15 14:28:22 +000045.. data:: BuiltinFunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000046 BuiltinMethodType
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandld66a0292008-08-23 15:14:57 +000048 The type of built-in functions like :func:`len` or :func:`sys.exit`, and
49 methods of built-in classes. (Here, the term "built-in" means "written in
50 C".)
Georg Brandl116aa622007-08-15 14:28:22 +000051
52
Georg Brandl116aa622007-08-15 14:28:22 +000053.. data:: ModuleType
54
55 The type of modules.
56
57
Georg Brandl116aa622007-08-15 14:28:22 +000058.. data:: TracebackType
59
60 The type of traceback objects such as found in ``sys.exc_info()[2]``.
61
62
63.. data:: FrameType
64
65 The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
66 traceback object.
67
68
Georg Brandl116aa622007-08-15 14:28:22 +000069.. data:: GetSetDescriptorType
70
Christian Heimes5e696852008-04-09 08:37:03 +000071 The type of objects defined in extension modules with ``PyGetSetDef``, such
72 as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
73 descriptor for object attributes; it has the same purpose as the
74 :class:`property` type, but for classes defined in extension modules.
Georg Brandl116aa622007-08-15 14:28:22 +000075
Georg Brandl116aa622007-08-15 14:28:22 +000076
77.. data:: MemberDescriptorType
78
Christian Heimes5e696852008-04-09 08:37:03 +000079 The type of objects defined in extension modules with ``PyMemberDef``, such
80 as ``datetime.timedelta.days``. This type is used as descriptor for simple C
81 data members which use standard conversion functions; it has the same purpose
82 as the :class:`property` type, but for classes defined in extension modules.
Georg Brandl495f7b52009-10-27 15:28:25 +000083
84 .. impl-detail::
85
86 In other implementations of Python, this type may be identical to
87 ``GetSetDescriptorType``.
Victor Stinner0db176f2012-04-16 00:16:30 +020088
89.. class:: MappingProxyType(mapping)
90
91 Read-only proxy of a mapping. It provides a dynamic view on the mapping's
92 entries, which means that when the mapping changes, the view reflects these
93 changes.
94
95 .. versionadded:: 3.3
96
97 .. describe:: key in proxy
98
99 Return ``True`` if the underlying mapping has a key *key*, else
100 ``False``.
101
102 .. describe:: proxy[key]
103
104 Return the item of the underlying mapping with key *key*. Raises a
105 :exc:`KeyError` if *key* is not in the underlying mapping.
106
107 .. describe:: iter(proxy)
108
109 Return an iterator over the keys of the underlying mapping. This is a
110 shortcut for ``iter(proxy.keys())``.
111
112 .. describe:: len(proxy)
113
114 Return the number of items in the underlying mapping.
115
116 .. method:: copy()
117
118 Return a shallow copy of the underlying mapping.
119
120 .. method:: get(key[, default])
121
122 Return the value for *key* if *key* is in the underlying mapping, else
123 *default*. If *default* is not given, it defaults to ``None``, so that
124 this method never raises a :exc:`KeyError`.
125
126 .. method:: items()
127
128 Return a new view of the underlying mapping's items (``(key, value)``
129 pairs).
130
131 .. method:: keys()
132
133 Return a new view of the underlying mapping's keys.
134
135 .. method:: values()
136
137 Return a new view of the underlying mapping's values.
138
139