Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`types` --- Names for built-in types |
| 3 | ========================================= |
| 4 | |
| 5 | .. module:: types |
| 6 | :synopsis: Names for built-in types. |
| 7 | |
| 8 | |
| 9 | This module defines names for some object types that are used by the standard |
| 10 | Python interpreter, but not for the types defined by various extension modules. |
| 11 | Also, it does not include some of the types that arise during processing such as |
Collin Winter | 2befd24 | 2007-08-28 06:09:03 +0000 | [diff] [blame] | 12 | the ``listiterator`` type. New names exported by future versions of this module |
| 13 | will all end in ``Type``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
| 15 | Typical use is for functions that do different things depending on their |
| 16 | argument types, like the following:: |
| 17 | |
Collin Winter | 2befd24 | 2007-08-28 06:09:03 +0000 | [diff] [blame] | 18 | from types import IntType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | def delete(mylist, item): |
| 20 | if type(item) is IntType: |
| 21 | del mylist[item] |
| 22 | else: |
| 23 | mylist.remove(item) |
| 24 | |
| 25 | Starting in Python 2.2, built-in factory functions such as :func:`int` and |
| 26 | :func:`str` are also names for the corresponding types. This is now the |
| 27 | preferred way to access the type instead of using the :mod:`types` module. |
| 28 | Accordingly, the example above should be written as follows:: |
| 29 | |
| 30 | def delete(mylist, item): |
| 31 | if isinstance(item, int): |
| 32 | del mylist[item] |
| 33 | else: |
| 34 | mylist.remove(item) |
| 35 | |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame^] | 36 | Starting in Python 3.0 all types that are also available as builtins are no |
| 37 | longer exposed through the types module. |
| 38 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | The module defines the following names: |
| 40 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | .. data:: FunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 42 | LambdaType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | |
| 44 | The type of user-defined functions and lambdas. |
| 45 | |
| 46 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | .. data:: GeneratorType |
| 48 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 49 | The type of :term:`generator`-iterator objects, produced by calling a |
| 50 | generator function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | .. data:: CodeType |
| 54 | |
| 55 | .. index:: builtin: compile |
| 56 | |
| 57 | The type for code objects such as returned by :func:`compile`. |
| 58 | |
| 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | .. data:: MethodType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 61 | UnboundMethdType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
| 63 | The type of methods of user-defined class instances. |
| 64 | |
| 65 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 67 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| 69 | The type of built-in functions like :func:`len` or :func:`sys.exit`. |
| 70 | |
| 71 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | .. data:: ModuleType |
| 73 | |
| 74 | The type of modules. |
| 75 | |
| 76 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | .. data:: TracebackType |
| 78 | |
| 79 | The type of traceback objects such as found in ``sys.exc_info()[2]``. |
| 80 | |
| 81 | |
| 82 | .. data:: FrameType |
| 83 | |
| 84 | The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 85 | traceback object. |
| 86 | |
| 87 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | .. data:: DictProxyType |
| 89 | |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame^] | 90 | The type of dict proxies, such as ``type.__dict__``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | .. data:: GetSetDescriptorType |
| 94 | |
| 95 | The type of objects defined in extension modules with ``PyGetSetDef``, such as |
| 96 | ``FrameType.f_locals`` or ``array.array.typecode``. This constant is not |
| 97 | defined in implementations of Python that do not have such extension types, so |
| 98 | for portable code use ``hasattr(types, 'GetSetDescriptorType')``. |
| 99 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | .. data:: MemberDescriptorType |
| 102 | |
| 103 | The type of objects defined in extension modules with ``PyMemberDef``, such as |
| 104 | ``datetime.timedelta.days``. This constant is not defined in implementations of |
| 105 | Python that do not have such extension types, so for portable code use |
| 106 | ``hasattr(types, 'MemberDescriptorType')``. |