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-inspect.html" /> | |
13 | <link rel="prev" href="module-UserString.html" /> | |
14 | <link rel="parent" href="python.html" /> | |
15 | <link rel="next" href="operator-map.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.10 operator -- Standard operators as functions.</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.9 UserString " | |
25 | href="module-UserString.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.10.1 Mapping Operators to" | |
31 | href="operator-map.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="module-UserString.html">3.9 UserString </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="operator-map.html">3.10.1 Mapping Operators to</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0051000000000000000000"> | |
56 | 3.10 <tt class="module">operator</tt> -- | |
57 | Standard operators as functions.</A> | |
58 | </H1> | |
59 | <A NAME="module-operator"></A> | |
60 | ||
61 | <P> | |
62 | ||
63 | <P> | |
64 | The <tt class="module">operator</tt> module exports a set of functions implemented in C | |
65 | corresponding to the intrinsic operators of Python. For example, | |
66 | <code>operator.add(x, y)</code> is equivalent to the expression <code>x+y</code>. The | |
67 | function names are those used for special class methods; variants without | |
68 | leading and trailing "<tt class="samp">__</tt>" are also provided for convenience. | |
69 | ||
70 | <P> | |
71 | The functions fall into categories that perform object comparisons, | |
72 | logical operations, mathematical operations, sequence operations, and | |
73 | abstract type tests. | |
74 | ||
75 | <P> | |
76 | The object comparison functions are useful for all objects, and are | |
77 | named after the rich comparison operators they support: | |
78 | ||
79 | <P> | |
80 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
81 | <td><nobr><b><tt id='l2h-490' xml:id='l2h-490' class="function">lt</tt></b>(</nobr></td> | |
82 | <td><var>a, b</var>)</td></tr></table></dt> | |
83 | <dd> | |
84 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
85 | <td><nobr><b><tt class="function">le</tt></b>(</nobr></td> | |
86 | <td><var>a, b</var>)</td></tr></table></dt> | |
87 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
88 | <td><nobr><b><tt class="function">eq</tt></b>(</nobr></td> | |
89 | <td><var>a, b</var>)</td></tr></table></dt> | |
90 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
91 | <td><nobr><b><tt class="function">ne</tt></b>(</nobr></td> | |
92 | <td><var>a, b</var>)</td></tr></table></dt> | |
93 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
94 | <td><nobr><b><tt class="function">ge</tt></b>(</nobr></td> | |
95 | <td><var>a, b</var>)</td></tr></table></dt> | |
96 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
97 | <td><nobr><b><tt class="function">gt</tt></b>(</nobr></td> | |
98 | <td><var>a, b</var>)</td></tr></table></dt> | |
99 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
100 | <td><nobr><b><tt class="function">__lt__</tt></b>(</nobr></td> | |
101 | <td><var>a, b</var>)</td></tr></table></dt> | |
102 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
103 | <td><nobr><b><tt class="function">__le__</tt></b>(</nobr></td> | |
104 | <td><var>a, b</var>)</td></tr></table></dt> | |
105 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
106 | <td><nobr><b><tt class="function">__eq__</tt></b>(</nobr></td> | |
107 | <td><var>a, b</var>)</td></tr></table></dt> | |
108 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
109 | <td><nobr><b><tt class="function">__ne__</tt></b>(</nobr></td> | |
110 | <td><var>a, b</var>)</td></tr></table></dt> | |
111 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
112 | <td><nobr><b><tt class="function">__ge__</tt></b>(</nobr></td> | |
113 | <td><var>a, b</var>)</td></tr></table></dt> | |
114 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
115 | <td><nobr><b><tt class="function">__gt__</tt></b>(</nobr></td> | |
116 | <td><var>a, b</var>)</td></tr></table></dt> | |
117 | <dd>Perform ``rich comparisons'' between <var>a</var> and <var>b</var>. Specifically, | |
118 | <code>lt(<var>a</var>, <var>b</var>)</code> is equivalent to <code><var>a</var> < <var>b</var></code>, | |
119 | <code>le(<var>a</var>, <var>b</var>)</code> is equivalent to <code><var>a</var> <= <var>b</var></code>, | |
120 | <code>eq(<var>a</var>, <var>b</var>)</code> is equivalent to <code><var>a</var> == <var>b</var></code>, | |
121 | <code>ne(<var>a</var>, <var>b</var>)</code> is equivalent to <code><var>a</var> != <var>b</var></code>, | |
122 | <code>gt(<var>a</var>, <var>b</var>)</code> is equivalent to <code><var>a</var> > <var>b</var></code> | |
123 | and | |
124 | <code>ge(<var>a</var>, <var>b</var>)</code> is equivalent to <code><var>a</var> >= <var>b</var></code>. | |
125 | Note that unlike the built-in <tt class="function">cmp()</tt>, these functions can | |
126 | return any value, which may or may not be interpretable as a Boolean | |
127 | value. See the <em class="citetitle"><a | |
128 | href="../ref/ref.html" | |
129 | title="Python Reference Manual" | |
130 | >Python Reference Manual</a></em> | |
131 | for more information about rich comparisons. | |
132 | ||
133 | <span class="versionnote">New in version 2.2.</span> | |
134 | ||
135 | </dl> | |
136 | ||
137 | <P> | |
138 | The logical operations are also generally applicable to all objects, | |
139 | and support truth tests, identity tests, and boolean operations: | |
140 | ||
141 | <P> | |
142 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
143 | <td><nobr><b><tt id='l2h-502' xml:id='l2h-502' class="function">not_</tt></b>(</nobr></td> | |
144 | <td><var>o</var>)</td></tr></table></dt> | |
145 | <dd> | |
146 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
147 | <td><nobr><b><tt class="function">__not__</tt></b>(</nobr></td> | |
148 | <td><var>o</var>)</td></tr></table></dt> | |
149 | <dd>Return the outcome of <tt class="keyword">not</tt> <var>o</var>. (Note that there is no | |
150 | <tt class="method">__not__()</tt> method for object instances; only the interpreter | |
151 | core defines this operation. The result is affected by the | |
152 | <tt class="method">__nonzero__()</tt> and <tt class="method">__len__()</tt> methods.) | |
153 | </dl> | |
154 | ||
155 | <P> | |
156 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
157 | <td><nobr><b><tt id='l2h-504' xml:id='l2h-504' class="function">truth</tt></b>(</nobr></td> | |
158 | <td><var>o</var>)</td></tr></table></dt> | |
159 | <dd> | |
160 | Return <tt class="constant">True</tt> if <var>o</var> is true, and <tt class="constant">False</tt> | |
161 | otherwise. This is equivalent to using the <tt class="class">bool</tt> | |
162 | constructor. | |
163 | </dl> | |
164 | ||
165 | <P> | |
166 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
167 | <td><nobr><b><tt id='l2h-505' xml:id='l2h-505' class="function">is_</tt></b>(</nobr></td> | |
168 | <td><var>a, b</var>)</td></tr></table></dt> | |
169 | <dd> | |
170 | Return <code><var>a</var> is <var>b</var></code>. Tests object identity. | |
171 | ||
172 | <span class="versionnote">New in version 2.3.</span> | |
173 | ||
174 | </dl> | |
175 | ||
176 | <P> | |
177 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
178 | <td><nobr><b><tt id='l2h-506' xml:id='l2h-506' class="function">is_not</tt></b>(</nobr></td> | |
179 | <td><var>a, b</var>)</td></tr></table></dt> | |
180 | <dd> | |
181 | Return <code><var>a</var> is not <var>b</var></code>. Tests object identity. | |
182 | ||
183 | <span class="versionnote">New in version 2.3.</span> | |
184 | ||
185 | </dl> | |
186 | ||
187 | <P> | |
188 | The mathematical and bitwise operations are the most numerous: | |
189 | ||
190 | <P> | |
191 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
192 | <td><nobr><b><tt id='l2h-507' xml:id='l2h-507' class="function">abs</tt></b>(</nobr></td> | |
193 | <td><var>o</var>)</td></tr></table></dt> | |
194 | <dd> | |
195 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
196 | <td><nobr><b><tt class="function">__abs__</tt></b>(</nobr></td> | |
197 | <td><var>o</var>)</td></tr></table></dt> | |
198 | <dd>Return the absolute value of <var>o</var>. | |
199 | </dl> | |
200 | ||
201 | <P> | |
202 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
203 | <td><nobr><b><tt id='l2h-509' xml:id='l2h-509' class="function">add</tt></b>(</nobr></td> | |
204 | <td><var>a, b</var>)</td></tr></table></dt> | |
205 | <dd> | |
206 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
207 | <td><nobr><b><tt class="function">__add__</tt></b>(</nobr></td> | |
208 | <td><var>a, b</var>)</td></tr></table></dt> | |
209 | <dd>Return <var>a</var> <code>+</code> <var>b</var>, for <var>a</var> and <var>b</var> numbers. | |
210 | </dl> | |
211 | ||
212 | <P> | |
213 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
214 | <td><nobr><b><tt id='l2h-511' xml:id='l2h-511' class="function">and_</tt></b>(</nobr></td> | |
215 | <td><var>a, b</var>)</td></tr></table></dt> | |
216 | <dd> | |
217 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
218 | <td><nobr><b><tt class="function">__and__</tt></b>(</nobr></td> | |
219 | <td><var>a, b</var>)</td></tr></table></dt> | |
220 | <dd>Return the bitwise and of <var>a</var> and <var>b</var>. | |
221 | </dl> | |
222 | ||
223 | <P> | |
224 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
225 | <td><nobr><b><tt id='l2h-513' xml:id='l2h-513' class="function">div</tt></b>(</nobr></td> | |
226 | <td><var>a, b</var>)</td></tr></table></dt> | |
227 | <dd> | |
228 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
229 | <td><nobr><b><tt class="function">__div__</tt></b>(</nobr></td> | |
230 | <td><var>a, b</var>)</td></tr></table></dt> | |
231 | <dd>Return <var>a</var> <code>/</code> <var>b</var> when <code>__future__.division</code> is not | |
232 | in effect. This is also known as ``classic'' division. | |
233 | </dl> | |
234 | ||
235 | <P> | |
236 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
237 | <td><nobr><b><tt id='l2h-515' xml:id='l2h-515' class="function">floordiv</tt></b>(</nobr></td> | |
238 | <td><var>a, b</var>)</td></tr></table></dt> | |
239 | <dd> | |
240 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
241 | <td><nobr><b><tt class="function">__floordiv__</tt></b>(</nobr></td> | |
242 | <td><var>a, b</var>)</td></tr></table></dt> | |
243 | <dd>Return <var>a</var> <code>//</code> <var>b</var>. | |
244 | ||
245 | <span class="versionnote">New in version 2.2.</span> | |
246 | ||
247 | </dl> | |
248 | ||
249 | <P> | |
250 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
251 | <td><nobr><b><tt id='l2h-517' xml:id='l2h-517' class="function">inv</tt></b>(</nobr></td> | |
252 | <td><var>o</var>)</td></tr></table></dt> | |
253 | <dd> | |
254 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
255 | <td><nobr><b><tt class="function">invert</tt></b>(</nobr></td> | |
256 | <td><var>o</var>)</td></tr></table></dt> | |
257 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
258 | <td><nobr><b><tt class="function">__inv__</tt></b>(</nobr></td> | |
259 | <td><var>o</var>)</td></tr></table></dt> | |
260 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
261 | <td><nobr><b><tt class="function">__invert__</tt></b>(</nobr></td> | |
262 | <td><var>o</var>)</td></tr></table></dt> | |
263 | <dd>Return the bitwise inverse of the number <var>o</var>. This is equivalent | |
264 | to <code>~</code><var>o</var>. The names <tt class="function">invert()</tt> and | |
265 | <tt class="function">__invert__()</tt> were added in Python 2.0. | |
266 | </dl> | |
267 | ||
268 | <P> | |
269 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
270 | <td><nobr><b><tt id='l2h-521' xml:id='l2h-521' class="function">lshift</tt></b>(</nobr></td> | |
271 | <td><var>a, b</var>)</td></tr></table></dt> | |
272 | <dd> | |
273 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
274 | <td><nobr><b><tt class="function">__lshift__</tt></b>(</nobr></td> | |
275 | <td><var>a, b</var>)</td></tr></table></dt> | |
276 | <dd>Return <var>a</var> shifted left by <var>b</var>. | |
277 | </dl> | |
278 | ||
279 | <P> | |
280 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
281 | <td><nobr><b><tt id='l2h-523' xml:id='l2h-523' class="function">mod</tt></b>(</nobr></td> | |
282 | <td><var>a, b</var>)</td></tr></table></dt> | |
283 | <dd> | |
284 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
285 | <td><nobr><b><tt class="function">__mod__</tt></b>(</nobr></td> | |
286 | <td><var>a, b</var>)</td></tr></table></dt> | |
287 | <dd>Return <var>a</var> <code>%</code> <var>b</var>. | |
288 | </dl> | |
289 | ||
290 | <P> | |
291 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
292 | <td><nobr><b><tt id='l2h-525' xml:id='l2h-525' class="function">mul</tt></b>(</nobr></td> | |
293 | <td><var>a, b</var>)</td></tr></table></dt> | |
294 | <dd> | |
295 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
296 | <td><nobr><b><tt class="function">__mul__</tt></b>(</nobr></td> | |
297 | <td><var>a, b</var>)</td></tr></table></dt> | |
298 | <dd>Return <var>a</var> <code>*</code> <var>b</var>, for <var>a</var> and <var>b</var> numbers. | |
299 | </dl> | |
300 | ||
301 | <P> | |
302 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
303 | <td><nobr><b><tt id='l2h-527' xml:id='l2h-527' class="function">neg</tt></b>(</nobr></td> | |
304 | <td><var>o</var>)</td></tr></table></dt> | |
305 | <dd> | |
306 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
307 | <td><nobr><b><tt class="function">__neg__</tt></b>(</nobr></td> | |
308 | <td><var>o</var>)</td></tr></table></dt> | |
309 | <dd>Return <var>o</var> negated. | |
310 | </dl> | |
311 | ||
312 | <P> | |
313 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
314 | <td><nobr><b><tt id='l2h-529' xml:id='l2h-529' class="function">or_</tt></b>(</nobr></td> | |
315 | <td><var>a, b</var>)</td></tr></table></dt> | |
316 | <dd> | |
317 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
318 | <td><nobr><b><tt class="function">__or__</tt></b>(</nobr></td> | |
319 | <td><var>a, b</var>)</td></tr></table></dt> | |
320 | <dd>Return the bitwise or of <var>a</var> and <var>b</var>. | |
321 | </dl> | |
322 | ||
323 | <P> | |
324 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
325 | <td><nobr><b><tt id='l2h-531' xml:id='l2h-531' class="function">pos</tt></b>(</nobr></td> | |
326 | <td><var>o</var>)</td></tr></table></dt> | |
327 | <dd> | |
328 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
329 | <td><nobr><b><tt class="function">__pos__</tt></b>(</nobr></td> | |
330 | <td><var>o</var>)</td></tr></table></dt> | |
331 | <dd>Return <var>o</var> positive. | |
332 | </dl> | |
333 | ||
334 | <P> | |
335 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
336 | <td><nobr><b><tt id='l2h-533' xml:id='l2h-533' class="function">pow</tt></b>(</nobr></td> | |
337 | <td><var>a, b</var>)</td></tr></table></dt> | |
338 | <dd> | |
339 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
340 | <td><nobr><b><tt class="function">__pow__</tt></b>(</nobr></td> | |
341 | <td><var>a, b</var>)</td></tr></table></dt> | |
342 | <dd>Return <var>a</var> <code>**</code> <var>b</var>, for <var>a</var> and <var>b</var> numbers. | |
343 | ||
344 | <span class="versionnote">New in version 2.3.</span> | |
345 | ||
346 | </dl> | |
347 | ||
348 | <P> | |
349 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
350 | <td><nobr><b><tt id='l2h-535' xml:id='l2h-535' class="function">rshift</tt></b>(</nobr></td> | |
351 | <td><var>a, b</var>)</td></tr></table></dt> | |
352 | <dd> | |
353 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
354 | <td><nobr><b><tt class="function">__rshift__</tt></b>(</nobr></td> | |
355 | <td><var>a, b</var>)</td></tr></table></dt> | |
356 | <dd>Return <var>a</var> shifted right by <var>b</var>. | |
357 | </dl> | |
358 | ||
359 | <P> | |
360 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
361 | <td><nobr><b><tt id='l2h-537' xml:id='l2h-537' class="function">sub</tt></b>(</nobr></td> | |
362 | <td><var>a, b</var>)</td></tr></table></dt> | |
363 | <dd> | |
364 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
365 | <td><nobr><b><tt class="function">__sub__</tt></b>(</nobr></td> | |
366 | <td><var>a, b</var>)</td></tr></table></dt> | |
367 | <dd>Return <var>a</var> <code>-</code> <var>b</var>. | |
368 | </dl> | |
369 | ||
370 | <P> | |
371 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
372 | <td><nobr><b><tt id='l2h-539' xml:id='l2h-539' class="function">truediv</tt></b>(</nobr></td> | |
373 | <td><var>a, b</var>)</td></tr></table></dt> | |
374 | <dd> | |
375 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
376 | <td><nobr><b><tt class="function">__truediv__</tt></b>(</nobr></td> | |
377 | <td><var>a, b</var>)</td></tr></table></dt> | |
378 | <dd>Return <var>a</var> <code>/</code> <var>b</var> when <code>__future__.division</code> is in | |
379 | effect. This is also known as division. | |
380 | ||
381 | <span class="versionnote">New in version 2.2.</span> | |
382 | ||
383 | </dl> | |
384 | ||
385 | <P> | |
386 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
387 | <td><nobr><b><tt id='l2h-541' xml:id='l2h-541' class="function">xor</tt></b>(</nobr></td> | |
388 | <td><var>a, b</var>)</td></tr></table></dt> | |
389 | <dd> | |
390 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
391 | <td><nobr><b><tt class="function">__xor__</tt></b>(</nobr></td> | |
392 | <td><var>a, b</var>)</td></tr></table></dt> | |
393 | <dd>Return the bitwise exclusive or of <var>a</var> and <var>b</var>. | |
394 | </dl> | |
395 | ||
396 | <P> | |
397 | Operations which work with sequences include: | |
398 | ||
399 | <P> | |
400 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
401 | <td><nobr><b><tt id='l2h-543' xml:id='l2h-543' class="function">concat</tt></b>(</nobr></td> | |
402 | <td><var>a, b</var>)</td></tr></table></dt> | |
403 | <dd> | |
404 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
405 | <td><nobr><b><tt class="function">__concat__</tt></b>(</nobr></td> | |
406 | <td><var>a, b</var>)</td></tr></table></dt> | |
407 | <dd>Return <var>a</var> <code>+</code> <var>b</var> for <var>a</var> and <var>b</var> sequences. | |
408 | </dl> | |
409 | ||
410 | <P> | |
411 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
412 | <td><nobr><b><tt id='l2h-545' xml:id='l2h-545' class="function">contains</tt></b>(</nobr></td> | |
413 | <td><var>a, b</var>)</td></tr></table></dt> | |
414 | <dd> | |
415 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
416 | <td><nobr><b><tt class="function">__contains__</tt></b>(</nobr></td> | |
417 | <td><var>a, b</var>)</td></tr></table></dt> | |
418 | <dd>Return the outcome of the test <var>b</var> <code>in</code> <var>a</var>. | |
419 | Note the reversed operands. The name <tt class="function">__contains__()</tt> was | |
420 | added in Python 2.0. | |
421 | </dl> | |
422 | ||
423 | <P> | |
424 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
425 | <td><nobr><b><tt id='l2h-547' xml:id='l2h-547' class="function">countOf</tt></b>(</nobr></td> | |
426 | <td><var>a, b</var>)</td></tr></table></dt> | |
427 | <dd> | |
428 | Return the number of occurrences of <var>b</var> in <var>a</var>. | |
429 | </dl> | |
430 | ||
431 | <P> | |
432 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
433 | <td><nobr><b><tt id='l2h-548' xml:id='l2h-548' class="function">delitem</tt></b>(</nobr></td> | |
434 | <td><var>a, b</var>)</td></tr></table></dt> | |
435 | <dd> | |
436 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
437 | <td><nobr><b><tt class="function">__delitem__</tt></b>(</nobr></td> | |
438 | <td><var>a, b</var>)</td></tr></table></dt> | |
439 | <dd>Remove the value of <var>a</var> at index <var>b</var>. | |
440 | </dl> | |
441 | ||
442 | <P> | |
443 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
444 | <td><nobr><b><tt id='l2h-550' xml:id='l2h-550' class="function">delslice</tt></b>(</nobr></td> | |
445 | <td><var>a, b, c</var>)</td></tr></table></dt> | |
446 | <dd> | |
447 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
448 | <td><nobr><b><tt class="function">__delslice__</tt></b>(</nobr></td> | |
449 | <td><var>a, b, c</var>)</td></tr></table></dt> | |
450 | <dd>Delete the slice of <var>a</var> from index <var>b</var> to index <var>c</var><code>-1</code>. | |
451 | </dl> | |
452 | ||
453 | <P> | |
454 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
455 | <td><nobr><b><tt id='l2h-552' xml:id='l2h-552' class="function">getitem</tt></b>(</nobr></td> | |
456 | <td><var>a, b</var>)</td></tr></table></dt> | |
457 | <dd> | |
458 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
459 | <td><nobr><b><tt class="function">__getitem__</tt></b>(</nobr></td> | |
460 | <td><var>a, b</var>)</td></tr></table></dt> | |
461 | <dd>Return the value of <var>a</var> at index <var>b</var>. | |
462 | </dl> | |
463 | ||
464 | <P> | |
465 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
466 | <td><nobr><b><tt id='l2h-554' xml:id='l2h-554' class="function">getslice</tt></b>(</nobr></td> | |
467 | <td><var>a, b, c</var>)</td></tr></table></dt> | |
468 | <dd> | |
469 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
470 | <td><nobr><b><tt class="function">__getslice__</tt></b>(</nobr></td> | |
471 | <td><var>a, b, c</var>)</td></tr></table></dt> | |
472 | <dd>Return the slice of <var>a</var> from index <var>b</var> to index <var>c</var><code>-1</code>. | |
473 | </dl> | |
474 | ||
475 | <P> | |
476 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
477 | <td><nobr><b><tt id='l2h-556' xml:id='l2h-556' class="function">indexOf</tt></b>(</nobr></td> | |
478 | <td><var>a, b</var>)</td></tr></table></dt> | |
479 | <dd> | |
480 | Return the index of the first of occurrence of <var>b</var> in <var>a</var>. | |
481 | </dl> | |
482 | ||
483 | <P> | |
484 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
485 | <td><nobr><b><tt id='l2h-557' xml:id='l2h-557' class="function">repeat</tt></b>(</nobr></td> | |
486 | <td><var>a, b</var>)</td></tr></table></dt> | |
487 | <dd> | |
488 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
489 | <td><nobr><b><tt class="function">__repeat__</tt></b>(</nobr></td> | |
490 | <td><var>a, b</var>)</td></tr></table></dt> | |
491 | <dd>Return <var>a</var> <code>*</code> <var>b</var> where <var>a</var> is a sequence and | |
492 | <var>b</var> is an integer. | |
493 | </dl> | |
494 | ||
495 | <P> | |
496 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
497 | <td><nobr><b><tt id='l2h-559' xml:id='l2h-559' class="function">sequenceIncludes</tt></b>(</nobr></td> | |
498 | <td><var>...</var>)</td></tr></table></dt> | |
499 | <dd> | |
500 | <div class="versionnote"><b>Deprecated since release 2.0.</b> | |
501 | Use <tt class="function">contains()</tt> instead.</div><p></p> | |
502 | Alias for <tt class="function">contains()</tt>. | |
503 | </dl> | |
504 | ||
505 | <P> | |
506 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
507 | <td><nobr><b><tt id='l2h-560' xml:id='l2h-560' class="function">setitem</tt></b>(</nobr></td> | |
508 | <td><var>a, b, c</var>)</td></tr></table></dt> | |
509 | <dd> | |
510 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
511 | <td><nobr><b><tt class="function">__setitem__</tt></b>(</nobr></td> | |
512 | <td><var>a, b, c</var>)</td></tr></table></dt> | |
513 | <dd>Set the value of <var>a</var> at index <var>b</var> to <var>c</var>. | |
514 | </dl> | |
515 | ||
516 | <P> | |
517 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
518 | <td><nobr><b><tt id='l2h-562' xml:id='l2h-562' class="function">setslice</tt></b>(</nobr></td> | |
519 | <td><var>a, b, c, v</var>)</td></tr></table></dt> | |
520 | <dd> | |
521 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
522 | <td><nobr><b><tt class="function">__setslice__</tt></b>(</nobr></td> | |
523 | <td><var>a, b, c, v</var>)</td></tr></table></dt> | |
524 | <dd>Set the slice of <var>a</var> from index <var>b</var> to index <var>c</var><code>-1</code> to the | |
525 | sequence <var>v</var>. | |
526 | </dl> | |
527 | ||
528 | <P> | |
529 | The <tt class="module">operator</tt> module also defines a few predicates to test the | |
530 | type of objects. <span class="note"><b class="label">Note:</b> | |
531 | Be careful not to misinterpret the | |
532 | results of these functions; only <tt class="function">isCallable()</tt> has any | |
533 | measure of reliability with instance objects. For example:</span> | |
534 | ||
535 | <P> | |
536 | <div class="verbatim"><pre> | |
537 | >>> class C: | |
538 | ... pass | |
539 | ... | |
540 | >>> import operator | |
541 | >>> o = C() | |
542 | >>> operator.isMappingType(o) | |
543 | True | |
544 | </pre></div> | |
545 | ||
546 | <P> | |
547 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
548 | <td><nobr><b><tt id='l2h-564' xml:id='l2h-564' class="function">isCallable</tt></b>(</nobr></td> | |
549 | <td><var>o</var>)</td></tr></table></dt> | |
550 | <dd> | |
551 | <div class="versionnote"><b>Deprecated since release 2.0.</b> | |
552 | Use the <tt class="function">callable()</tt> built-in function instead.</div><p></p> | |
553 | Returns true if the object <var>o</var> can be called like a function, | |
554 | otherwise it returns false. True is returned for functions, bound and | |
555 | unbound methods, class objects, and instance objects which support the | |
556 | <tt class="method">__call__()</tt> method. | |
557 | </dl> | |
558 | ||
559 | <P> | |
560 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
561 | <td><nobr><b><tt id='l2h-565' xml:id='l2h-565' class="function">isMappingType</tt></b>(</nobr></td> | |
562 | <td><var>o</var>)</td></tr></table></dt> | |
563 | <dd> | |
564 | Returns true if the object <var>o</var> supports the mapping interface. | |
565 | This is true for dictionaries and all instance objects defining | |
566 | <tt class="method">__getitem__</tt>. | |
567 | <span class="warning"><b class="label">Warning:</b> | |
568 | There is no reliable way to test if an instance | |
569 | supports the complete mapping protocol since the interface itself is | |
570 | ill-defined. This makes this test less useful than it otherwise might | |
571 | be.</span> | |
572 | </dl> | |
573 | ||
574 | <P> | |
575 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
576 | <td><nobr><b><tt id='l2h-566' xml:id='l2h-566' class="function">isNumberType</tt></b>(</nobr></td> | |
577 | <td><var>o</var>)</td></tr></table></dt> | |
578 | <dd> | |
579 | Returns true if the object <var>o</var> represents a number. This is true | |
580 | for all numeric types implemented in C. | |
581 | <span class="warning"><b class="label">Warning:</b> | |
582 | There is no reliable way to test if an instance | |
583 | supports the complete numeric interface since the interface itself is | |
584 | ill-defined. This makes this test less useful than it otherwise might | |
585 | be.</span> | |
586 | </dl> | |
587 | ||
588 | <P> | |
589 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
590 | <td><nobr><b><tt id='l2h-567' xml:id='l2h-567' class="function">isSequenceType</tt></b>(</nobr></td> | |
591 | <td><var>o</var>)</td></tr></table></dt> | |
592 | <dd> | |
593 | Returns true if the object <var>o</var> supports the sequence protocol. | |
594 | This returns true for all objects which define sequence methods in C, | |
595 | and for all instance objects defining <tt class="method">__getitem__</tt>. | |
596 | <span class="warning"><b class="label">Warning:</b> | |
597 | There is no reliable | |
598 | way to test if an instance supports the complete sequence interface | |
599 | since the interface itself is ill-defined. This makes this test less | |
600 | useful than it otherwise might be.</span> | |
601 | </dl> | |
602 | ||
603 | <P> | |
604 | Example: Build a dictionary that maps the ordinals from <code>0</code> to | |
605 | <code>255</code> to their character equivalents. | |
606 | ||
607 | <P> | |
608 | <div class="verbatim"><pre> | |
609 | >>> import operator | |
610 | >>> d = {} | |
611 | >>> keys = range(256) | |
612 | >>> vals = map(chr, keys) | |
613 | >>> map(operator.setitem, [d]*len(keys), keys, vals) | |
614 | </pre></div> | |
615 | ||
616 | <P> | |
617 | The <tt class="module">operator</tt> module also defines tools for generalized attribute | |
618 | and item lookups. These are useful for making fast field extractors | |
619 | as arguments for <tt class="function">map()</tt>, <tt class="function">sorted()</tt>, | |
620 | <tt class="method">itertools.groupby()</tt>, or other functions that expect a | |
621 | function argument. | |
622 | ||
623 | <P> | |
624 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
625 | <td><nobr><b><tt id='l2h-568' xml:id='l2h-568' class="function">attrgetter</tt></b>(</nobr></td> | |
626 | <td><var>attr</var>)</td></tr></table></dt> | |
627 | <dd> | |
628 | Return a callable object that fetches <var>attr</var> from its operand. | |
629 | After, "<tt class="samp">f=attrgetter('name')</tt>", the call "<tt class="samp">f(b)</tt>" returns | |
630 | "<tt class="samp">b.name</tt>". | |
631 | ||
632 | <span class="versionnote">New in version 2.4.</span> | |
633 | ||
634 | </dl> | |
635 | ||
636 | <P> | |
637 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
638 | <td><nobr><b><tt id='l2h-569' xml:id='l2h-569' class="function">itemgetter</tt></b>(</nobr></td> | |
639 | <td><var>item</var>)</td></tr></table></dt> | |
640 | <dd> | |
641 | Return a callable object that fetches <var>item</var> from its operand. | |
642 | After, "<tt class="samp">f=itemgetter(2)</tt>", the call "<tt class="samp">f(b)</tt>" returns | |
643 | "<tt class="samp">b[2]</tt>". | |
644 | ||
645 | <span class="versionnote">New in version 2.4.</span> | |
646 | ||
647 | </dl> | |
648 | ||
649 | <P> | |
650 | Examples: | |
651 | ||
652 | <P> | |
653 | <div class="verbatim"><pre> | |
654 | >>> from operator import * | |
655 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] | |
656 | >>> getcount = itemgetter(1) | |
657 | >>> map(getcount, inventory) | |
658 | [3, 2, 5, 1] | |
659 | >>> sorted(inventory, key=getcount) | |
660 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] | |
661 | </pre></div> | |
662 | ||
663 | <P> | |
664 | ||
665 | <p><br /></p><hr class='online-navigation' /> | |
666 | <div class='online-navigation'> | |
667 | <!--Table of Child-Links--> | |
668 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
669 | ||
670 | <UL CLASS="ChildLinks"> | |
671 | <LI><A href="operator-map.html">3.10.1 Mapping Operators to Functions</a> | |
672 | </ul> | |
673 | <!--End of Table of Child-Links--> | |
674 | </div> | |
675 | ||
676 | <DIV CLASS="navigation"> | |
677 | <div class='online-navigation'> | |
678 | <p></p><hr /> | |
679 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
680 | <tr> | |
681 | <td class='online-navigation'><a rel="prev" title="3.9 UserString " | |
682 | href="module-UserString.html"><img src='../icons/previous.png' | |
683 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
684 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
685 | href="python.html"><img src='../icons/up.png' | |
686 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
687 | <td class='online-navigation'><a rel="next" title="3.10.1 Mapping Operators to" | |
688 | href="operator-map.html"><img src='../icons/next.png' | |
689 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
690 | <td align="center" width="100%">Python Library Reference</td> | |
691 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
692 | href="contents.html"><img src='../icons/contents.png' | |
693 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
694 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
695 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
696 | <td class='online-navigation'><a rel="index" title="Index" | |
697 | href="genindex.html"><img src='../icons/index.png' | |
698 | border='0' height='32' alt='Index' width='32' /></A></td> | |
699 | </tr></table> | |
700 | <div class='online-navigation'> | |
701 | <b class="navlabel">Previous:</b> | |
702 | <a class="sectref" rel="prev" href="module-UserString.html">3.9 UserString </A> | |
703 | <b class="navlabel">Up:</b> | |
704 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
705 | <b class="navlabel">Next:</b> | |
706 | <a class="sectref" rel="next" href="operator-map.html">3.10.1 Mapping Operators to</A> | |
707 | </div> | |
708 | </div> | |
709 | <hr /> | |
710 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
711 | </DIV> | |
712 | <!--End of Navigation Panel--> | |
713 | <ADDRESS> | |
714 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
715 | </ADDRESS> | |
716 | </BODY> | |
717 | </HTML> |