blob: 48940676473099527cbb7e2edea8f741c6948b84 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`types` --- Names for built-in types
3=========================================
4
5.. module:: types
6 :synopsis: Names for built-in types.
7
8
9This module defines names for some object types that are used by the standard
10Python interpreter, but not for the types defined by various extension modules.
11Also, it does not include some of the types that arise during processing such as
Collin Winter2befd242007-08-28 06:09:03 +000012the ``listiterator`` type. New names exported by future versions of this module
13will all end in ``Type``.
Georg Brandl116aa622007-08-15 14:28:22 +000014
15Typical use is for functions that do different things depending on their
16argument types, like the following::
17
Collin Winter2befd242007-08-28 06:09:03 +000018 from types import IntType
Georg Brandl116aa622007-08-15 14:28:22 +000019 def delete(mylist, item):
20 if type(item) is IntType:
21 del mylist[item]
22 else:
23 mylist.remove(item)
24
25Starting 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
27preferred way to access the type instead of using the :mod:`types` module.
28Accordingly, 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 Heimesc9543e42007-11-28 08:28:28 +000036Starting in Python 3.0 all types that are also available as builtins are no
37longer exposed through the types module.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039The module defines the following names:
40
Georg Brandl116aa622007-08-15 14:28:22 +000041.. data:: FunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000042 LambdaType
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 The type of user-defined functions and lambdas.
45
46
Georg Brandl116aa622007-08-15 14:28:22 +000047.. data:: GeneratorType
48
Georg Brandl9afde1c2007-11-01 20:32:30 +000049 The type of :term:`generator`-iterator objects, produced by calling a
50 generator function.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. data:: CodeType
54
55 .. index:: builtin: compile
56
57 The type for code objects such as returned by :func:`compile`.
58
59
Georg Brandl116aa622007-08-15 14:28:22 +000060.. data:: MethodType
Georg Brandla1e9ef72007-09-04 07:23:09 +000061 UnboundMethdType
Georg Brandl116aa622007-08-15 14:28:22 +000062
63 The type of methods of user-defined class instances.
64
65
Georg Brandl116aa622007-08-15 14:28:22 +000066.. data:: BuiltinFunctionType
Georg Brandla1e9ef72007-09-04 07:23:09 +000067 BuiltinMethodType
Georg Brandl116aa622007-08-15 14:28:22 +000068
69 The type of built-in functions like :func:`len` or :func:`sys.exit`.
70
71
Georg Brandl116aa622007-08-15 14:28:22 +000072.. data:: ModuleType
73
74 The type of modules.
75
76
Georg Brandl116aa622007-08-15 14:28:22 +000077.. 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 Brandl116aa622007-08-15 14:28:22 +000088.. data:: DictProxyType
89
Christian Heimesc9543e42007-11-28 08:28:28 +000090 The type of dict proxies, such as ``type.__dict__``.
Georg Brandl116aa622007-08-15 14:28:22 +000091
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 Brandl116aa622007-08-15 14:28:22 +0000100
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')``.