Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-UserDict.html" /> | |
13 | <link rel="prev" href="module-atexit.html" /> | |
14 | <link rel="parent" href="python.html" /> | |
15 | <link rel="next" href="module-UserDict.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.6 types -- Names for built-in types</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="3.5.1 atexit Example" | |
25 | href="atexit-example.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
28 | href="python.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="3.7 UserDict " | |
31 | href="module-UserDict.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="atexit-example.html">3.5.1 atexit Example</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-UserDict.html">3.7 UserDict </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION005600000000000000000"> | |
56 | 3.6 <tt class="module">types</tt> -- | |
57 | Names for built-in types</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-types"></A> | |
62 | ||
63 | <P> | |
64 | This module defines names for some object types that are used by | |
65 | the standard Python interpreter, but not for the types defined by various | |
66 | extension modules. Also, it does not include some of the types that | |
67 | arise during processing such the <code>listiterator</code> type. | |
68 | It is safe to use "<tt class="samp">from types import *</tt>" -- | |
69 | the module does not export any names besides the ones listed here. | |
70 | New names exported by future versions of this module will all end in | |
71 | "<tt class="samp">Type</tt>". | |
72 | ||
73 | <P> | |
74 | Typical use is for functions that do different things depending on | |
75 | their argument types, like the following: | |
76 | ||
77 | <P> | |
78 | <div class="verbatim"><pre> | |
79 | from types import * | |
80 | def delete(mylist, item): | |
81 | if type(item) is IntType: | |
82 | del mylist[item] | |
83 | else: | |
84 | mylist.remove(item) | |
85 | </pre></div> | |
86 | ||
87 | <P> | |
88 | Starting in Python 2.2, built-in factory functions such as | |
89 | <tt class="function">int()</tt> and <tt class="function">str()</tt> are also names for the | |
90 | corresponding types. This is now the preferred way to access | |
91 | the type instead of using the <tt class="module">types</tt> module. Accordingly, | |
92 | the example above should be written as follows: | |
93 | ||
94 | <P> | |
95 | <div class="verbatim"><pre> | |
96 | def delete(mylist, item): | |
97 | if isinstance(item, int): | |
98 | del mylist[item] | |
99 | else: | |
100 | mylist.remove(item) | |
101 | </pre></div> | |
102 | ||
103 | <P> | |
104 | The module defines the following names: | |
105 | ||
106 | <P> | |
107 | <dl><dt><b><tt id='l2h-440' xml:id='l2h-440'>NoneType</tt></b></dt> | |
108 | <dd> | |
109 | The type of <code>None</code>. | |
110 | </dd></dl> | |
111 | ||
112 | <P> | |
113 | <dl><dt><b><tt id='l2h-441' xml:id='l2h-441'>TypeType</tt></b></dt> | |
114 | <dd> | |
115 | The type of type objects (such as returned by | |
116 | <tt class="function">type()</tt><a id='l2h-442' xml:id='l2h-442'></a>). | |
117 | </dd></dl> | |
118 | ||
119 | <P> | |
120 | <dl><dt><b><tt id='l2h-443' xml:id='l2h-443'>BooleanType</tt></b></dt> | |
121 | <dd> | |
122 | The type of the <tt class="class">bool</tt> values <code>True</code> and <code>False</code>; this | |
123 | is an alias of the built-in <tt class="function">bool()</tt> function. | |
124 | ||
125 | <span class="versionnote">New in version 2.3.</span> | |
126 | ||
127 | </dd></dl> | |
128 | ||
129 | <P> | |
130 | <dl><dt><b><tt id='l2h-444' xml:id='l2h-444'>IntType</tt></b></dt> | |
131 | <dd> | |
132 | The type of integers (e.g. <code>1</code>). | |
133 | </dd></dl> | |
134 | ||
135 | <P> | |
136 | <dl><dt><b><tt id='l2h-445' xml:id='l2h-445'>LongType</tt></b></dt> | |
137 | <dd> | |
138 | The type of long integers (e.g. <code>1L</code>). | |
139 | </dd></dl> | |
140 | ||
141 | <P> | |
142 | <dl><dt><b><tt id='l2h-446' xml:id='l2h-446'>FloatType</tt></b></dt> | |
143 | <dd> | |
144 | The type of floating point numbers (e.g. <code>1.0</code>). | |
145 | </dd></dl> | |
146 | ||
147 | <P> | |
148 | <dl><dt><b><tt id='l2h-447' xml:id='l2h-447'>ComplexType</tt></b></dt> | |
149 | <dd> | |
150 | The type of complex numbers (e.g. <code>1.0j</code>). This is not defined | |
151 | if Python was built without complex number support. | |
152 | </dd></dl> | |
153 | ||
154 | <P> | |
155 | <dl><dt><b><tt id='l2h-448' xml:id='l2h-448'>StringType</tt></b></dt> | |
156 | <dd> | |
157 | The type of character strings (e.g. <code>'Spam'</code>). | |
158 | </dd></dl> | |
159 | ||
160 | <P> | |
161 | <dl><dt><b><tt id='l2h-449' xml:id='l2h-449'>UnicodeType</tt></b></dt> | |
162 | <dd> | |
163 | The type of Unicode character strings (e.g. <code>u'Spam'</code>). This is | |
164 | not defined if Python was built without Unicode support. | |
165 | </dd></dl> | |
166 | ||
167 | <P> | |
168 | <dl><dt><b><tt id='l2h-450' xml:id='l2h-450'>TupleType</tt></b></dt> | |
169 | <dd> | |
170 | The type of tuples (e.g. <code>(1, 2, 3, 'Spam')</code>). | |
171 | </dd></dl> | |
172 | ||
173 | <P> | |
174 | <dl><dt><b><tt id='l2h-451' xml:id='l2h-451'>ListType</tt></b></dt> | |
175 | <dd> | |
176 | The type of lists (e.g. <code>[0, 1, 2, 3]</code>). | |
177 | </dd></dl> | |
178 | ||
179 | <P> | |
180 | <dl><dt><b><tt id='l2h-452' xml:id='l2h-452'>DictType</tt></b></dt> | |
181 | <dd> | |
182 | The type of dictionaries (e.g. <code>{'Bacon': 1, 'Ham': 0}</code>). | |
183 | </dd></dl> | |
184 | ||
185 | <P> | |
186 | <dl><dt><b><tt id='l2h-453' xml:id='l2h-453'>DictionaryType</tt></b></dt> | |
187 | <dd> | |
188 | An alternate name for <code>DictType</code>. | |
189 | </dd></dl> | |
190 | ||
191 | <P> | |
192 | <dl><dt><b><tt id='l2h-454' xml:id='l2h-454'>FunctionType</tt></b></dt> | |
193 | <dd> | |
194 | The type of user-defined functions and lambdas. | |
195 | </dd></dl> | |
196 | ||
197 | <P> | |
198 | <dl><dt><b><tt id='l2h-455' xml:id='l2h-455'>LambdaType</tt></b></dt> | |
199 | <dd> | |
200 | An alternate name for <code>FunctionType</code>. | |
201 | </dd></dl> | |
202 | ||
203 | <P> | |
204 | <dl><dt><b><tt id='l2h-456' xml:id='l2h-456'>GeneratorType</tt></b></dt> | |
205 | <dd> | |
206 | The type of generator-iterator objects, produced by calling a | |
207 | generator function. | |
208 | ||
209 | <span class="versionnote">New in version 2.2.</span> | |
210 | ||
211 | </dd></dl> | |
212 | ||
213 | <P> | |
214 | <dl><dt><b><tt id='l2h-457' xml:id='l2h-457'>CodeType</tt></b></dt> | |
215 | <dd> | |
216 | The type for code objects such as returned by | |
217 | <tt class="function">compile()</tt><a id='l2h-458' xml:id='l2h-458'></a>. | |
218 | </dd></dl> | |
219 | ||
220 | <P> | |
221 | <dl><dt><b><tt id='l2h-459' xml:id='l2h-459'>ClassType</tt></b></dt> | |
222 | <dd> | |
223 | The type of user-defined classes. | |
224 | </dd></dl> | |
225 | ||
226 | <P> | |
227 | <dl><dt><b><tt id='l2h-460' xml:id='l2h-460'>InstanceType</tt></b></dt> | |
228 | <dd> | |
229 | The type of instances of user-defined classes. | |
230 | </dd></dl> | |
231 | ||
232 | <P> | |
233 | <dl><dt><b><tt id='l2h-461' xml:id='l2h-461'>MethodType</tt></b></dt> | |
234 | <dd> | |
235 | The type of methods of user-defined class instances. | |
236 | </dd></dl> | |
237 | ||
238 | <P> | |
239 | <dl><dt><b><tt id='l2h-462' xml:id='l2h-462'>UnboundMethodType</tt></b></dt> | |
240 | <dd> | |
241 | An alternate name for <code>MethodType</code>. | |
242 | </dd></dl> | |
243 | ||
244 | <P> | |
245 | <dl><dt><b><tt id='l2h-463' xml:id='l2h-463'>BuiltinFunctionType</tt></b></dt> | |
246 | <dd> | |
247 | The type of built-in functions like <tt class="function">len()</tt> or | |
248 | <tt class="function">sys.exit()</tt>. | |
249 | </dd></dl> | |
250 | ||
251 | <P> | |
252 | <dl><dt><b><tt id='l2h-464' xml:id='l2h-464'>BuiltinMethodType</tt></b></dt> | |
253 | <dd> | |
254 | An alternate name for <code>BuiltinFunction</code>. | |
255 | </dd></dl> | |
256 | ||
257 | <P> | |
258 | <dl><dt><b><tt id='l2h-465' xml:id='l2h-465'>ModuleType</tt></b></dt> | |
259 | <dd> | |
260 | The type of modules. | |
261 | </dd></dl> | |
262 | ||
263 | <P> | |
264 | <dl><dt><b><tt id='l2h-466' xml:id='l2h-466'>FileType</tt></b></dt> | |
265 | <dd> | |
266 | The type of open file objects such as <code>sys.stdout</code>. | |
267 | </dd></dl> | |
268 | ||
269 | <P> | |
270 | <dl><dt><b><tt id='l2h-467' xml:id='l2h-467'>XRangeType</tt></b></dt> | |
271 | <dd> | |
272 | The type of range objects returned by | |
273 | <tt class="function">xrange()</tt><a id='l2h-468' xml:id='l2h-468'></a>. | |
274 | </dd></dl> | |
275 | ||
276 | <P> | |
277 | <dl><dt><b><tt id='l2h-469' xml:id='l2h-469'>SliceType</tt></b></dt> | |
278 | <dd> | |
279 | The type of objects returned by | |
280 | <tt class="function">slice()</tt><a id='l2h-470' xml:id='l2h-470'></a>. | |
281 | </dd></dl> | |
282 | ||
283 | <P> | |
284 | <dl><dt><b><tt id='l2h-471' xml:id='l2h-471'>EllipsisType</tt></b></dt> | |
285 | <dd> | |
286 | The type of <code>Ellipsis</code>. | |
287 | </dd></dl> | |
288 | ||
289 | <P> | |
290 | <dl><dt><b><tt id='l2h-472' xml:id='l2h-472'>TracebackType</tt></b></dt> | |
291 | <dd> | |
292 | The type of traceback objects such as found in | |
293 | <code>sys.exc_traceback</code>. | |
294 | </dd></dl> | |
295 | ||
296 | <P> | |
297 | <dl><dt><b><tt id='l2h-473' xml:id='l2h-473'>FrameType</tt></b></dt> | |
298 | <dd> | |
299 | The type of frame objects such as found in <code>tb.tb_frame</code> if | |
300 | <code>tb</code> is a traceback object. | |
301 | </dd></dl> | |
302 | ||
303 | <P> | |
304 | <dl><dt><b><tt id='l2h-474' xml:id='l2h-474'>BufferType</tt></b></dt> | |
305 | <dd> | |
306 | The type of buffer objects created by the | |
307 | <tt class="function">buffer()</tt><a id='l2h-475' xml:id='l2h-475'></a> function. | |
308 | </dd></dl> | |
309 | ||
310 | <P> | |
311 | <dl><dt><b><tt id='l2h-476' xml:id='l2h-476'>StringTypes</tt></b></dt> | |
312 | <dd> | |
313 | A sequence containing <code>StringType</code> and <code>UnicodeType</code> used to | |
314 | facilitate easier checking for any string object. Using this is more | |
315 | portable than using a sequence of the two string types constructed | |
316 | elsewhere since it only contains <code>UnicodeType</code> if it has been | |
317 | built in the running version of Python. For example: | |
318 | <code>isinstance(s, types.StringTypes)</code>. | |
319 | ||
320 | <span class="versionnote">New in version 2.2.</span> | |
321 | ||
322 | </dd></dl> | |
323 | ||
324 | <DIV CLASS="navigation"> | |
325 | <div class='online-navigation'> | |
326 | <p></p><hr /> | |
327 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
328 | <tr> | |
329 | <td class='online-navigation'><a rel="prev" title="3.5.1 atexit Example" | |
330 | href="atexit-example.html"><img src='../icons/previous.png' | |
331 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
332 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
333 | href="python.html"><img src='../icons/up.png' | |
334 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
335 | <td class='online-navigation'><a rel="next" title="3.7 UserDict " | |
336 | href="module-UserDict.html"><img src='../icons/next.png' | |
337 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
338 | <td align="center" width="100%">Python Library Reference</td> | |
339 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
340 | href="contents.html"><img src='../icons/contents.png' | |
341 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
342 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
343 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
344 | <td class='online-navigation'><a rel="index" title="Index" | |
345 | href="genindex.html"><img src='../icons/index.png' | |
346 | border='0' height='32' alt='Index' width='32' /></A></td> | |
347 | </tr></table> | |
348 | <div class='online-navigation'> | |
349 | <b class="navlabel">Previous:</b> | |
350 | <a class="sectref" rel="prev" href="atexit-example.html">3.5.1 atexit Example</A> | |
351 | <b class="navlabel">Up:</b> | |
352 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
353 | <b class="navlabel">Next:</b> | |
354 | <a class="sectref" rel="next" href="module-UserDict.html">3.7 UserDict </A> | |
355 | </div> | |
356 | </div> | |
357 | <hr /> | |
358 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
359 | </DIV> | |
360 | <!--End of Navigation Panel--> | |
361 | <ADDRESS> | |
362 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
363 | </ADDRESS> | |
364 | </BODY> | |
365 | </HTML> |