Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{types} --- |
Raymond Hettinger | 6fe1299 | 2003-01-10 09:33:08 +0000 | [diff] [blame] | 2 | Names for built-in types} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 3 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{types} |
Raymond Hettinger | 6fe1299 | 2003-01-10 09:33:08 +0000 | [diff] [blame] | 5 | \modulesynopsis{Names for built-in types.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | |
Neal Norwitz | 3cb68a2 | 2003-01-10 13:52:30 +0000 | [diff] [blame] | 8 | This module defines names for some object types that are used by |
Raymond Hettinger | 6fe1299 | 2003-01-10 09:33:08 +0000 | [diff] [blame] | 9 | the standard Python interpreter, but not for the types defined by various |
| 10 | extension modules. Also, it does not include some of the types that |
| 11 | arise during processing such the \code{listiterator} type. |
| 12 | It is safe to use \samp{from types import *} --- |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 13 | the module does not export any names besides the ones listed here. |
| 14 | New names exported by future versions of this module will all end in |
| 15 | \samp{Type}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 16 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 17 | Typical use is for functions that do different things depending on |
| 18 | their argument types, like the following: |
Fred Drake | 1a855fa | 1998-03-16 05:23:50 +0000 | [diff] [blame] | 19 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 20 | \begin{verbatim} |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 21 | from types import * |
Raymond Hettinger | 6fe1299 | 2003-01-10 09:33:08 +0000 | [diff] [blame] | 22 | def delete(mylist, item): |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 23 | if type(item) is IntType: |
Raymond Hettinger | 6fe1299 | 2003-01-10 09:33:08 +0000 | [diff] [blame] | 24 | del mylist[item] |
| 25 | else: |
| 26 | mylist.remove(item) |
| 27 | \end{verbatim} |
| 28 | |
| 29 | Starting in Python 2.2, built-in factory functions such as |
| 30 | \function{int()} and \function{str()} are also names for the |
| 31 | corresponding types. This is now the preferred way to access |
| 32 | the type instead of using the \module{types} module. Accordingly, |
| 33 | the example above should be written as follows: |
| 34 | |
| 35 | \begin{verbatim} |
| 36 | def delete(mylist, item): |
| 37 | if isinstance(item, int): |
Neal Norwitz | 3cb68a2 | 2003-01-10 13:52:30 +0000 | [diff] [blame] | 38 | del mylist[item] |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 39 | else: |
Neal Norwitz | 3cb68a2 | 2003-01-10 13:52:30 +0000 | [diff] [blame] | 40 | mylist.remove(item) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 41 | \end{verbatim} |
Fred Drake | 1a855fa | 1998-03-16 05:23:50 +0000 | [diff] [blame] | 42 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 43 | The module defines the following names: |
Guido van Rossum | 1738311 | 1994-04-21 10:32:28 +0000 | [diff] [blame] | 44 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 45 | \begin{datadesc}{NoneType} |
| 46 | The type of \code{None}. |
| 47 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 48 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 49 | \begin{datadesc}{TypeType} |
| 50 | The type of type objects (such as returned by |
| 51 | \function{type()}\bifuncindex{type}). |
| 52 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | |
Fred Drake | c585075 | 2002-05-22 02:44:24 +0000 | [diff] [blame] | 54 | \begin{datadesc}{BooleanType} |
| 55 | The type of the \class{bool} values \code{True} and \code{False}; this |
| 56 | is an alias of the built-in \function{bool()} function. |
| 57 | \versionadded{2.3} |
| 58 | \end{datadesc} |
| 59 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 60 | \begin{datadesc}{IntType} |
| 61 | The type of integers (e.g. \code{1}). |
| 62 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 63 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 64 | \begin{datadesc}{LongType} |
| 65 | The type of long integers (e.g. \code{1L}). |
| 66 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 68 | \begin{datadesc}{FloatType} |
| 69 | The type of floating point numbers (e.g. \code{1.0}). |
| 70 | \end{datadesc} |
Guido van Rossum | 3a0d850 | 1997-06-02 17:18:00 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 1eb200d | 1998-07-24 15:01:05 +0000 | [diff] [blame] | 72 | \begin{datadesc}{ComplexType} |
Fred Drake | 5ecb7aa | 2002-05-21 14:28:22 +0000 | [diff] [blame] | 73 | The type of complex numbers (e.g. \code{1.0j}). This is not defined |
| 74 | if Python was built without complex number support. |
Guido van Rossum | 1eb200d | 1998-07-24 15:01:05 +0000 | [diff] [blame] | 75 | \end{datadesc} |
| 76 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 77 | \begin{datadesc}{StringType} |
| 78 | The type of character strings (e.g. \code{'Spam'}). |
| 79 | \end{datadesc} |
Guido van Rossum | 3a0d850 | 1997-06-02 17:18:00 +0000 | [diff] [blame] | 80 | |
Fred Drake | 0b72116 | 2000-04-06 15:05:04 +0000 | [diff] [blame] | 81 | \begin{datadesc}{UnicodeType} |
Fred Drake | 5ecb7aa | 2002-05-21 14:28:22 +0000 | [diff] [blame] | 82 | The type of Unicode character strings (e.g. \code{u'Spam'}). This is |
| 83 | not defined if Python was built without Unicode support. |
Fred Drake | 0b72116 | 2000-04-06 15:05:04 +0000 | [diff] [blame] | 84 | \end{datadesc} |
| 85 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 86 | \begin{datadesc}{TupleType} |
| 87 | The type of tuples (e.g. \code{(1, 2, 3, 'Spam')}). |
| 88 | \end{datadesc} |
Guido van Rossum | 97ea116 | 1998-06-30 15:56:23 +0000 | [diff] [blame] | 89 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 90 | \begin{datadesc}{ListType} |
| 91 | The type of lists (e.g. \code{[0, 1, 2, 3]}). |
| 92 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 93 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 94 | \begin{datadesc}{DictType} |
| 95 | The type of dictionaries (e.g. \code{\{'Bacon': 1, 'Ham': 0\}}). |
| 96 | \end{datadesc} |
Fred Drake | c3e4549 | 1998-02-19 20:22:13 +0000 | [diff] [blame] | 97 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 98 | \begin{datadesc}{DictionaryType} |
| 99 | An alternate name for \code{DictType}. |
| 100 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 101 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 102 | \begin{datadesc}{FunctionType} |
| 103 | The type of user-defined functions and lambdas. |
| 104 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 105 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 106 | \begin{datadesc}{LambdaType} |
| 107 | An alternate name for \code{FunctionType}. |
| 108 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 109 | |
Tim Peters | 3e7b1a0 | 2001-06-25 19:46:25 +0000 | [diff] [blame] | 110 | \begin{datadesc}{GeneratorType} |
| 111 | The type of generator-iterator objects, produced by calling a |
| 112 | generator function. |
| 113 | \versionadded{2.2} |
| 114 | \end{datadesc} |
| 115 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 116 | \begin{datadesc}{CodeType} |
| 117 | The type for code objects such as returned by |
| 118 | \function{compile()}\bifuncindex{compile}. |
| 119 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 120 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 121 | \begin{datadesc}{ClassType} |
| 122 | The type of user-defined classes. |
| 123 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 125 | \begin{datadesc}{InstanceType} |
| 126 | The type of instances of user-defined classes. |
| 127 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 128 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 129 | \begin{datadesc}{MethodType} |
| 130 | The type of methods of user-defined class instances. |
| 131 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 132 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 133 | \begin{datadesc}{UnboundMethodType} |
| 134 | An alternate name for \code{MethodType}. |
| 135 | \end{datadesc} |
Guido van Rossum | 6102b51 | 1997-05-28 19:32:11 +0000 | [diff] [blame] | 136 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 137 | \begin{datadesc}{BuiltinFunctionType} |
| 138 | The type of built-in functions like \function{len()} or |
| 139 | \function{sys.exit()}. |
| 140 | \end{datadesc} |
Barry Warsaw | dc0f00a | 1997-10-06 17:50:48 +0000 | [diff] [blame] | 141 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 142 | \begin{datadesc}{BuiltinMethodType} |
| 143 | An alternate name for \code{BuiltinFunction}. |
| 144 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 145 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 146 | \begin{datadesc}{ModuleType} |
| 147 | The type of modules. |
| 148 | \end{datadesc} |
Fred Drake | c3e4549 | 1998-02-19 20:22:13 +0000 | [diff] [blame] | 149 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 150 | \begin{datadesc}{FileType} |
| 151 | The type of open file objects such as \code{sys.stdout}. |
| 152 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 153 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 154 | \begin{datadesc}{XRangeType} |
| 155 | The type of range objects returned by |
| 156 | \function{xrange()}\bifuncindex{xrange}. |
| 157 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | 1eb200d | 1998-07-24 15:01:05 +0000 | [diff] [blame] | 159 | \begin{datadesc}{SliceType} |
| 160 | The type of objects returned by |
| 161 | \function{slice()}\bifuncindex{slice}. |
| 162 | \end{datadesc} |
| 163 | |
| 164 | \begin{datadesc}{EllipsisType} |
| 165 | The type of \code{Ellipsis}. |
| 166 | \end{datadesc} |
| 167 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 168 | \begin{datadesc}{TracebackType} |
| 169 | The type of traceback objects such as found in |
| 170 | \code{sys.exc_traceback}. |
| 171 | \end{datadesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 172 | |
Fred Drake | 78a6ddb | 1998-07-24 14:27:22 +0000 | [diff] [blame] | 173 | \begin{datadesc}{FrameType} |
| 174 | The type of frame objects such as found in \code{tb.tb_frame} if |
| 175 | \code{tb} is a traceback object. |
| 176 | \end{datadesc} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 177 | |
| 178 | \begin{datadesc}{BufferType} |
| 179 | The type of buffer objects created by the |
| 180 | \function{buffer()}\bifuncindex{buffer} function. |
| 181 | \end{datadesc} |
Skip Montanaro | c7ba0c4 | 2001-09-29 13:49:41 +0000 | [diff] [blame] | 182 | |
| 183 | \begin{datadesc}{StringTypes} |
Fred Drake | 5ecb7aa | 2002-05-21 14:28:22 +0000 | [diff] [blame] | 184 | A sequence containing \code{StringType} and \code{UnicodeType} used to |
| 185 | facilitate easier checking for any string object. Using this is more |
| 186 | portable than using a sequence of the two string types constructed |
| 187 | elsewhere since it only contains \code{UnicodeType} if it has been |
| 188 | built in the running version of Python. For example: |
| 189 | \code{isinstance(s, types.StringTypes)}. |
Fred Drake | c585075 | 2002-05-22 02:44:24 +0000 | [diff] [blame] | 190 | \versionadded{2.2} |
Skip Montanaro | c7ba0c4 | 2001-09-29 13:49:41 +0000 | [diff] [blame] | 191 | \end{datadesc} |