Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{types}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-types} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 3 | \stmodindex{types} |
| 4 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame^] | 5 | \setindexsubitem{(in module types)} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 7 | This module defines names for all object types that are used by the |
| 8 | standard Python interpreter (but not for the types defined by various |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 9 | extension modules). It is safe to use ``\code{from types import *}'' --- |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 10 | the module does not export any other names besides the ones listed |
| 11 | here. New names exported by future versions of this module will |
| 12 | all end in \code{Type}. |
| 13 | |
| 14 | Typical use is for functions that do different things depending on |
| 15 | their argument types, like the following: |
| 16 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame^] | 17 | \begin{verbatim} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 18 | from types import * |
| 19 | def delete(list, item): |
| 20 | if type(item) is IntType: |
| 21 | del list[item] |
| 22 | else: |
| 23 | list.remove(item) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame^] | 24 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 25 | % |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 26 | The module defines the following names: |
| 27 | |
| 28 | \begin{datadesc}{NoneType} |
| 29 | The type of \code{None}. |
| 30 | \end{datadesc} |
| 31 | |
| 32 | \begin{datadesc}{TypeType} |
| 33 | The type of type objects (such as returned by \code{type()}). |
| 34 | \end{datadesc} |
| 35 | |
| 36 | \begin{datadesc}{IntType} |
| 37 | The type of integers (e.g. \code{1}). |
| 38 | \end{datadesc} |
| 39 | |
| 40 | \begin{datadesc}{LongType} |
| 41 | The type of long integers (e.g. \code{1L}). |
| 42 | \end{datadesc} |
| 43 | |
| 44 | \begin{datadesc}{FloatType} |
| 45 | The type of floating point numbers (e.g. \code{1.0}). |
| 46 | \end{datadesc} |
| 47 | |
| 48 | \begin{datadesc}{StringType} |
| 49 | The type of character strings (e.g. \code{'Spam'}). |
| 50 | \end{datadesc} |
| 51 | |
| 52 | \begin{datadesc}{TupleType} |
| 53 | The type of tuples (e.g. \code{(1, 2, 3, 'Spam')}). |
| 54 | \end{datadesc} |
| 55 | |
| 56 | \begin{datadesc}{ListType} |
| 57 | The type of lists (e.g. \code{[0, 1, 2, 3]}). |
| 58 | \end{datadesc} |
| 59 | |
| 60 | \begin{datadesc}{DictType} |
| 61 | The type of dictionaries (e.g. \code{\{'Bacon': 1, 'Ham': 0\}}). |
| 62 | \end{datadesc} |
| 63 | |
| 64 | \begin{datadesc}{DictionaryType} |
| 65 | An alternative name for \code{DictType}. |
| 66 | \end{datadesc} |
| 67 | |
| 68 | \begin{datadesc}{FunctionType} |
| 69 | The type of user-defined functions and lambdas. |
| 70 | \end{datadesc} |
| 71 | |
| 72 | \begin{datadesc}{LambdaType} |
| 73 | An alternative name for \code{FunctionType}. |
| 74 | \end{datadesc} |
| 75 | |
| 76 | \begin{datadesc}{CodeType} |
| 77 | The type for code objects such as returned by \code{compile()}. |
| 78 | \end{datadesc} |
| 79 | |
| 80 | \begin{datadesc}{ClassType} |
| 81 | The type of user-defined classes. |
| 82 | \end{datadesc} |
| 83 | |
| 84 | \begin{datadesc}{InstanceType} |
| 85 | The type of instances of user-defined classes. |
| 86 | \end{datadesc} |
| 87 | |
| 88 | \begin{datadesc}{MethodType} |
| 89 | The type of methods of user-defined class instances. |
| 90 | \end{datadesc} |
| 91 | |
| 92 | \begin{datadesc}{UnboundMethodType} |
| 93 | An alternative name for \code{MethodType}. |
| 94 | \end{datadesc} |
| 95 | |
| 96 | \begin{datadesc}{BuiltinFunctionType} |
| 97 | The type of built-in functions like \code{len} or \code{sys.exit}. |
| 98 | \end{datadesc} |
| 99 | |
| 100 | \begin{datadesc}{BuiltinMethodType} |
| 101 | An alternative name for \code{BuiltinFunction}. |
| 102 | \end{datadesc} |
| 103 | |
| 104 | \begin{datadesc}{ModuleType} |
| 105 | The type of modules. |
| 106 | \end{datadesc} |
| 107 | |
| 108 | \begin{datadesc}{FileType} |
| 109 | The type of open file objects such as \code{sys.stdout}. |
| 110 | \end{datadesc} |
| 111 | |
| 112 | \begin{datadesc}{XRangeType} |
| 113 | The type of range objects returned by \code{xrange()}. |
| 114 | \end{datadesc} |
| 115 | |
| 116 | \begin{datadesc}{TracebackType} |
| 117 | The type of traceback objects such as found in \code{sys.exc_traceback}. |
| 118 | \end{datadesc} |
| 119 | |
| 120 | \begin{datadesc}{FrameType} |
| 121 | The type of frame objects such as found in \code{tb.tb_frame} if |
| 122 | \code{tb} is a traceback object. |
| 123 | \end{datadesc} |