Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`types` --- Names for built-in types |
| 2 | ========================================= |
| 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 | |
| 11 | This module defines names for some object types that are used by the standard |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 12 | Python 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 |
| 14 | transparently during processing such as the ``listiterator`` type. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 16 | Typical use is for :func:`isinstance` or :func:`issubclass` checks. |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 17 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | The module defines the following names: |
| 19 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | .. data:: FunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 21 | LambdaType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 23 | The type of user-defined functions and functions created by :keyword:`lambda` |
| 24 | expressions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | .. data:: GeneratorType |
| 28 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 29 | The type of :term:`generator`-iterator objects, produced by calling a |
| 30 | generator function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | .. data:: CodeType |
| 34 | |
| 35 | .. index:: builtin: compile |
| 36 | |
| 37 | The type for code objects such as returned by :func:`compile`. |
| 38 | |
| 39 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | .. data:: MethodType |
| 41 | |
| 42 | The type of methods of user-defined class instances. |
| 43 | |
| 44 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 46 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Georg Brandl | d66a029 | 2008-08-23 15:14:57 +0000 | [diff] [blame] | 48 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | .. data:: ModuleType |
| 54 | |
| 55 | The type of modules. |
| 56 | |
| 57 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | .. data:: GetSetDescriptorType |
| 70 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 71 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | .. data:: MemberDescriptorType |
| 78 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 79 | 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 Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 83 | |
| 84 | .. impl-detail:: |
| 85 | |
| 86 | In other implementations of Python, this type may be identical to |
| 87 | ``GetSetDescriptorType``. |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame^] | 88 | |
| 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 | |