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 |
| 61 | |
| 62 | The type of methods of user-defined class instances. |
| 63 | |
| 64 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | .. data:: BuiltinFunctionType |
Georg Brandl | a1e9ef7 | 2007-09-04 07:23:09 +0000 | [diff] [blame] | 66 | BuiltinMethodType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | The type of built-in functions like :func:`len` or :func:`sys.exit`. |
| 69 | |
| 70 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | .. data:: ModuleType |
| 72 | |
| 73 | The type of modules. |
| 74 | |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | .. data:: TracebackType |
| 77 | |
| 78 | The type of traceback objects such as found in ``sys.exc_info()[2]``. |
| 79 | |
| 80 | |
| 81 | .. data:: FrameType |
| 82 | |
| 83 | The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a |
| 84 | traceback object. |
| 85 | |
| 86 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | .. data:: GetSetDescriptorType |
| 88 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame^] | 89 | The type of objects defined in extension modules with ``PyGetSetDef``, such |
| 90 | as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as |
| 91 | descriptor for object attributes; it has the same purpose as the |
| 92 | :class:`property` type, but for classes defined in extension modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
| 95 | .. data:: MemberDescriptorType |
| 96 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame^] | 97 | The type of objects defined in extension modules with ``PyMemberDef``, such |
| 98 | as ``datetime.timedelta.days``. This type is used as descriptor for simple C |
| 99 | data members which use standard conversion functions; it has the same purpose |
| 100 | as the :class:`property` type, but for classes defined in extension modules. |
| 101 | In other implementations of Python, this type may be identical to |
| 102 | ``GetSetDescriptorType``. |