| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{\module{operator} --- | 
					
						
							|  |  |  |          Standard operators as functions.} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | \declaremodule{builtin}{operator} | 
					
						
							| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \sectionauthor{Skip Montanaro}{skip@automatrix.com} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \modulesynopsis{All Python's standard operators as built-in functions.} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | The \module{operator} module exports a set of functions implemented in C | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | corresponding to the intrinsic operators of Python.  For example, | 
					
						
							| 
									
										
										
										
											1997-12-29 17:11:55 +00:00
										 |  |  | \code{operator.add(x, y)} is equivalent to the expression \code{x+y}.  The | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | function names are those used for special class methods; variants without | 
					
						
							| 
									
										
										
										
											1997-12-04 14:20:59 +00:00
										 |  |  | leading and trailing \samp{__} are also provided for convenience. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | The functions fall into categories that perform object comparisons, | 
					
						
							|  |  |  | logical operations, mathematical operations, sequence operations, and | 
					
						
							|  |  |  | abstract type tests. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The object comparison functions are useful for all objects, and are | 
					
						
							|  |  |  | named after the rich comparison operators they support: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{lt}{a, b} | 
					
						
							|  |  |  | \funcline{le}{a, b} | 
					
						
							|  |  |  | \funcline{eq}{a, b} | 
					
						
							|  |  |  | \funcline{ne}{a, b} | 
					
						
							|  |  |  | \funcline{ge}{a, b} | 
					
						
							|  |  |  | \funcline{gt}{a, b} | 
					
						
							|  |  |  | \funcline{__lt__}{a, b} | 
					
						
							|  |  |  | \funcline{__le__}{a, b} | 
					
						
							|  |  |  | \funcline{__eq__}{a, b} | 
					
						
							|  |  |  | \funcline{__ne__}{a, b} | 
					
						
							|  |  |  | \funcline{__ge__}{a, b} | 
					
						
							|  |  |  | \funcline{__gt__}{a, b} | 
					
						
							|  |  |  | Perform ``rich comparisons'' between \var{a} and \var{b}. Specifically, | 
					
						
							|  |  |  | \code{lt(\var{a}, \var{b})} is equivalent to \code{\var{a} < \var{b}}, | 
					
						
							|  |  |  | \code{le(\var{a}, \var{b})} is equivalent to \code{\var{a} <= \var{b}}, | 
					
						
							|  |  |  | \code{eq(\var{a}, \var{b})} is equivalent to \code{\var{a} == \var{b}}, | 
					
						
							|  |  |  | \code{ne(\var{a}, \var{b})} is equivalent to \code{\var{a} != \var{b}}, | 
					
						
							|  |  |  | \code{gt(\var{a}, \var{b})} is equivalent to \code{\var{a} > \var{b}} | 
					
						
							|  |  |  | and | 
					
						
							|  |  |  | \code{ge(\var{a}, \var{b})} is equivalent to \code{\var{a} >= \var{b}}. | 
					
						
							|  |  |  | Note that unlike the built-in \function{cmp()}, these functions can | 
					
						
							|  |  |  | return any value, which may or may not be interpretable as a Boolean | 
					
						
							|  |  |  | value.  See the \citetitle[../ref/ref.html]{Python Reference Manual} | 
					
						
							|  |  |  | for more informations about rich comparisons. | 
					
						
							|  |  |  | \versionadded{2.2} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The logical operations are also generally applicable to all objects, | 
					
						
							| 
									
										
										
										
											2003-02-21 05:42:13 +00:00
										 |  |  | and support truth tests, identity tests, and boolean operations: | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{not_}{o} | 
					
						
							|  |  |  | \funcline{__not__}{o} | 
					
						
							|  |  |  | Return the outcome of \keyword{not} \var{o}.  (Note that there is no | 
					
						
							|  |  |  | \method{__not__()} method for object instances; only the interpreter | 
					
						
							|  |  |  | core defines this operation.  The result is affected by the | 
					
						
							|  |  |  | \method{__nonzero__()} and \method{__len__()} methods.) | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{truth}{o} | 
					
						
							| 
									
										
										
										
											2003-01-11 23:15:47 +00:00
										 |  |  | Return \constant{True} if \var{o} is true, and \constant{False} | 
					
						
							| 
									
										
										
										
											2003-01-12 15:04:54 +00:00
										 |  |  | otherwise.  This is equivalent to using the \class{bool} | 
					
						
							| 
									
										
										
										
											2003-01-11 23:15:47 +00:00
										 |  |  | constructor. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-18 23:22:20 +00:00
										 |  |  | \begin{funcdesc}{is_}{a, b} | 
					
						
							|  |  |  | Return \code{\var{a} is \var{b}}.  Tests object identity. | 
					
						
							| 
									
										
										
										
											2003-11-02 09:50:56 +00:00
										 |  |  | \versionadded{2.3} | 
					
						
							| 
									
										
										
										
											2003-01-18 23:22:20 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{is_not}{a, b} | 
					
						
							|  |  |  | Return \code{\var{a} is not \var{b}}.  Tests object identity. | 
					
						
							| 
									
										
										
										
											2003-11-02 09:50:56 +00:00
										 |  |  | \versionadded{2.3} | 
					
						
							| 
									
										
										
										
											2003-01-18 23:22:20 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | The mathematical and bitwise operations are the most numerous: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{abs}{o} | 
					
						
							|  |  |  | \funcline{__abs__}{o} | 
					
						
							|  |  |  | Return the absolute value of \var{o}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{add}{a, b} | 
					
						
							|  |  |  | \funcline{__add__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{and_}{a, b} | 
					
						
							|  |  |  | \funcline{__and__}{a, b} | 
					
						
							|  |  |  | Return the bitwise and of \var{a} and \var{b}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{div}{a, b} | 
					
						
							|  |  |  | \funcline{__div__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{/} \var{b} when \code{__future__.division} is not | 
					
						
							|  |  |  | in effect.  This is also known as ``classic'' division. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{floordiv}{a, b} | 
					
						
							|  |  |  | \funcline{__floordiv__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{//} \var{b}. | 
					
						
							|  |  |  | \versionadded{2.2} | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{inv}{o} | 
					
						
							| 
									
										
										
										
											2000-10-22 03:19:30 +00:00
										 |  |  | \funcline{invert}{o} | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | \funcline{__inv__}{o} | 
					
						
							| 
									
										
										
										
											2000-09-17 16:10:25 +00:00
										 |  |  | \funcline{__invert__}{o} | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | Return the bitwise inverse of the number \var{o}.  This is equivalent | 
					
						
							|  |  |  | to \code{\textasciitilde}\var{o}.  The names \function{invert()} and | 
					
						
							|  |  |  | \function{__invert__()} were added in Python 2.0. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-04 14:20:59 +00:00
										 |  |  | \begin{funcdesc}{lshift}{a, b} | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | \funcline{__lshift__}{a, b} | 
					
						
							| 
									
										
										
										
											1997-12-16 14:29:48 +00:00
										 |  |  | Return \var{a} shifted left by \var{b}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{mod}{a, b} | 
					
						
							|  |  |  | \funcline{__mod__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{\%} \var{b}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{mul}{a, b} | 
					
						
							|  |  |  | \funcline{__mul__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{neg}{o} | 
					
						
							|  |  |  | \funcline{__neg__}{o} | 
					
						
							|  |  |  | Return \var{o} negated. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-04 14:20:59 +00:00
										 |  |  | \begin{funcdesc}{or_}{a, b} | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | \funcline{__or__}{a, b} | 
					
						
							| 
									
										
										
										
											1997-12-16 14:29:48 +00:00
										 |  |  | Return the bitwise or of \var{a} and \var{b}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{pos}{o} | 
					
						
							|  |  |  | \funcline{__pos__}{o} | 
					
						
							|  |  |  | Return \var{o} positive. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-19 03:19:09 +00:00
										 |  |  | \begin{funcdesc}{pow}{a, b} | 
					
						
							|  |  |  | \funcline{__pow__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{**} \var{b}, for \var{a} and \var{b} numbers. | 
					
						
							| 
									
										
										
										
											2002-08-19 22:38:01 +00:00
										 |  |  | \versionadded{2.3} | 
					
						
							| 
									
										
										
										
											2002-08-19 03:19:09 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{rshift}{a, b} | 
					
						
							|  |  |  | \funcline{__rshift__}{a, b} | 
					
						
							|  |  |  | Return \var{a} shifted right by \var{b}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{sub}{a, b} | 
					
						
							|  |  |  | \funcline{__sub__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{-} \var{b}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{truediv}{a, b} | 
					
						
							|  |  |  | \funcline{__truediv__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{/} \var{b} when \code{__future__.division} is in | 
					
						
							|  |  |  | effect.  This is also known as division. | 
					
						
							|  |  |  | \versionadded{2.2} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-05-22 18:48:37 +00:00
										 |  |  | \begin{funcdesc}{xor}{a, b} | 
					
						
							|  |  |  | \funcline{__xor__}{a, b} | 
					
						
							|  |  |  | Return the bitwise exclusive or of \var{a} and \var{b}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | Operations which work with sequences include: | 
					
						
							| 
									
										
										
										
											1998-05-22 18:48:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-04 14:20:59 +00:00
										 |  |  | \begin{funcdesc}{concat}{a, b} | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | \funcline{__concat__}{a, b} | 
					
						
							| 
									
										
										
										
											1997-12-16 14:29:48 +00:00
										 |  |  | Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-05-22 18:48:37 +00:00
										 |  |  | \begin{funcdesc}{contains}{a, b} | 
					
						
							| 
									
										
										
										
											2000-09-17 16:10:25 +00:00
										 |  |  | \funcline{__contains__}{a, b} | 
					
						
							| 
									
										
										
										
											1998-05-22 18:48:37 +00:00
										 |  |  | Return the outcome of the test \var{b} \code{in} \var{a}. | 
					
						
							| 
									
										
										
										
											2000-09-17 16:10:25 +00:00
										 |  |  | Note the reversed operands.  The name \function{__contains__()} was | 
					
						
							|  |  |  | added in Python 2.0. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-05-22 18:48:37 +00:00
										 |  |  | \begin{funcdesc}{countOf}{a, b} | 
					
						
							|  |  |  | Return the number of occurrences of \var{b} in \var{a}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{delitem}{a, b} | 
					
						
							|  |  |  | \funcline{__delitem__}{a, b} | 
					
						
							|  |  |  | Remove the value of \var{a} at index \var{b}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{delslice}{a, b, c} | 
					
						
							|  |  |  | \funcline{__delslice__}{a, b, c} | 
					
						
							|  |  |  | Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}. | 
					
						
							| 
									
										
										
										
											1998-05-22 18:48:37 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-04 14:20:59 +00:00
										 |  |  | \begin{funcdesc}{getitem}{a, b} | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | \funcline{__getitem__}{a, b} | 
					
						
							| 
									
										
										
										
											1997-12-16 14:29:48 +00:00
										 |  |  | Return the value of \var{a} at index \var{b}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{getslice}{a, b, c} | 
					
						
							|  |  |  | \funcline{__getslice__}{a, b, c} | 
					
						
							|  |  |  | Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{indexOf}{a, b} | 
					
						
							|  |  |  | Return the index of the first of occurrence of \var{b} in \var{a}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | \begin{funcdesc}{repeat}{a, b} | 
					
						
							|  |  |  | \funcline{__repeat__}{a, b} | 
					
						
							|  |  |  | Return \var{a} \code{*} \var{b} where \var{a} is a sequence and | 
					
						
							|  |  |  | \var{b} is an integer. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{sequenceIncludes}{\unspecified} | 
					
						
							|  |  |  | \deprecated{2.0}{Use \function{contains()} instead.} | 
					
						
							|  |  |  | Alias for \function{contains()}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{setitem}{a, b, c} | 
					
						
							|  |  |  | \funcline{__setitem__}{a, b, c} | 
					
						
							|  |  |  | Set the value of \var{a} at index \var{b} to \var{c}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-04 14:20:59 +00:00
										 |  |  | \begin{funcdesc}{setslice}{a, b, c, v} | 
					
						
							| 
									
										
										
										
											1998-03-08 05:56:15 +00:00
										 |  |  | \funcline{__setslice__}{a, b, c, v} | 
					
						
							| 
									
										
										
										
											1997-12-16 14:29:48 +00:00
										 |  |  | Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the | 
					
						
							|  |  |  | sequence \var{v}. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  | The \module{operator} module also defines a few predicates to test the | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | type of objects.  \note{Be careful not to misinterpret the | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | results of these functions; only \function{isCallable()} has any | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | measure of reliability with instance objects.  For example:} | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> class C: | 
					
						
							|  |  |  | ...     pass | 
					
						
							|  |  |  | ...  | 
					
						
							|  |  |  | >>> import operator | 
					
						
							|  |  |  | >>> o = C() | 
					
						
							|  |  |  | >>> operator.isMappingType(o) | 
					
						
							| 
									
										
										
										
											2003-11-27 19:48:03 +00:00
										 |  |  | True | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{isCallable}{o} | 
					
						
							|  |  |  | \deprecated{2.0}{Use the \function{callable()} built-in function instead.} | 
					
						
							|  |  |  | Returns true if the object \var{o} can be called like a function, | 
					
						
							|  |  |  | otherwise it returns false.  True is returned for functions, bound and | 
					
						
							|  |  |  | unbound methods, class objects, and instance objects which support the | 
					
						
							|  |  |  | \method{__call__()} method. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{isMappingType}{o} | 
					
						
							|  |  |  | Returns true if the object \var{o} supports the mapping interface. | 
					
						
							|  |  |  | This is true for dictionaries and all instance objects. | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | \warning{There is no reliable way to test if an instance | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | supports the complete mapping protocol since the interface itself is | 
					
						
							|  |  |  | ill-defined.  This makes this test less useful than it otherwise might | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | be.} | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{isNumberType}{o} | 
					
						
							|  |  |  | Returns true if the object \var{o} represents a number.  This is true | 
					
						
							|  |  |  | for all numeric types implemented in C, and for all instance objects. | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | \warning{There is no reliable way to test if an instance | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | supports the complete numeric interface since the interface itself is | 
					
						
							|  |  |  | ill-defined.  This makes this test less useful than it otherwise might | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | be.} | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{isSequenceType}{o} | 
					
						
							|  |  |  | Returns true if the object \var{o} supports the sequence protocol. | 
					
						
							|  |  |  | This returns true for all objects which define sequence methods in C, | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | and for all instance objects.  \warning{There is no reliable | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | way to test if an instance supports the complete sequence interface | 
					
						
							|  |  |  | since the interface itself is ill-defined.  This makes this test less | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | useful than it otherwise might be.} | 
					
						
							| 
									
										
										
										
											2000-10-02 03:36:18 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-16 14:29:48 +00:00
										 |  |  | Example: Build a dictionary that maps the ordinals from \code{0} to | 
					
						
							|  |  |  | \code{256} to their character equivalents. | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1996-12-06 21:22:41 +00:00
										 |  |  | >>> import operator | 
					
						
							|  |  |  | >>> d = {} | 
					
						
							|  |  |  | >>> keys = range(256) | 
					
						
							|  |  |  | >>> vals = map(chr, keys) | 
					
						
							|  |  |  | >>> map(operator.setitem, [d]*len(keys), keys, vals) | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											2000-10-22 03:19:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-01 13:18:39 +00:00
										 |  |  | The \module{operator} module also defines tools for generalized attribute | 
					
						
							|  |  |  | and item lookups.  These are useful for making fast field extractors | 
					
						
							| 
									
										
										
										
											2003-12-17 20:50:46 +00:00
										 |  |  | as arguments for \function{map()}, \function{sorted()}, | 
					
						
							| 
									
										
										
										
											2003-12-01 13:18:39 +00:00
										 |  |  | \method{itertools.groupby()}, or other functions that expect a | 
					
						
							|  |  |  | function argument. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{attrgetter}{attr} | 
					
						
							|  |  |  | Return a callable object that fetches \var{attr} from its operand. | 
					
						
							|  |  |  | After, \samp{f=attrgetter('name')}, the call \samp{f(b)} returns | 
					
						
							|  |  |  | \samp{b.name}. | 
					
						
							|  |  |  | \versionadded{2.4} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | \begin{funcdesc}{itemgetter}{item} | 
					
						
							|  |  |  | Return a callable object that fetches \var{item} from its operand. | 
					
						
							|  |  |  | After, \samp{f=itemgetter(2)}, the call \samp{f(b)} returns | 
					
						
							|  |  |  | \samp{b[2]}. | 
					
						
							|  |  |  | \versionadded{2.4} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Examples: | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> from operator import * | 
					
						
							|  |  |  | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] | 
					
						
							|  |  |  | >>> getcount = itemgetter(1) | 
					
						
							|  |  |  | >>> map(getcount, inventory) | 
					
						
							|  |  |  | [3, 2, 5, 1] | 
					
						
							| 
									
										
										
										
											2003-12-17 20:50:46 +00:00
										 |  |  | >>> sorted(inventory, key=getcount) | 
					
						
							| 
									
										
										
										
											2003-12-01 13:18:39 +00:00
										 |  |  | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-22 03:19:30 +00:00
										 |  |  | \subsection{Mapping Operators to Functions \label{operator-map}} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This table shows how abstract operations correspond to operator | 
					
						
							|  |  |  | symbols in the Python syntax and the functions in the | 
					
						
							|  |  |  | \refmodule{operator} module. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function} | 
					
						
							|  |  |  |   \lineiii{Addition}{\code{\var{a} + \var{b}}} | 
					
						
							|  |  |  |           {\code{add(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}} | 
					
						
							|  |  |  |           {\code{concat(\var{seq1}, \var{seq2})}} | 
					
						
							|  |  |  |   \lineiii{Containment Test}{\code{\var{o} in \var{seq}}} | 
					
						
							|  |  |  |           {\code{contains(\var{seq}, \var{o})}} | 
					
						
							|  |  |  |   \lineiii{Division}{\code{\var{a} / \var{b}}} | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  |           {\code{div(\var{a}, \var{b}) \#} without \code{__future__.division}} | 
					
						
							|  |  |  |   \lineiii{Division}{\code{\var{a} / \var{b}}} | 
					
						
							|  |  |  |           {\code{truediv(\var{a}, \var{b}) \#} with \code{__future__.division}} | 
					
						
							|  |  |  |   \lineiii{Division}{\code{\var{a} // \var{b}}} | 
					
						
							|  |  |  |           {\code{floordiv(\var{a}, \var{b})}} | 
					
						
							| 
									
										
										
										
											2000-10-22 03:19:30 +00:00
										 |  |  |   \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}} | 
					
						
							|  |  |  |           {\code{and_(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}} | 
					
						
							|  |  |  |           {\code{xor(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}} | 
					
						
							|  |  |  |           {\code{invert(\var{a})}} | 
					
						
							|  |  |  |   \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}} | 
					
						
							|  |  |  |           {\code{or_(\var{a}, \var{b})}} | 
					
						
							| 
									
										
										
										
											2002-08-19 03:19:09 +00:00
										 |  |  |   \lineiii{Exponentiation}{\code{\var{a} ** \var{b}}} | 
					
						
							|  |  |  |           {\code{pow(\var{a}, \var{b})}} | 
					
						
							| 
									
										
										
										
											2003-02-21 05:42:13 +00:00
										 |  |  |   \lineiii{Identity}{\code{\var{a} is \var{b}}} | 
					
						
							|  |  |  |           {\code{is_(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Identity}{\code{\var{a} is not \var{b}}} | 
					
						
							|  |  |  |           {\code{is_not(\var{a}, \var{b})}} | 
					
						
							| 
									
										
										
										
											2000-10-22 03:19:30 +00:00
										 |  |  |   \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}} | 
					
						
							|  |  |  |           {\code{setitem(\var{o}, \var{k}, \var{v})}} | 
					
						
							|  |  |  |   \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}} | 
					
						
							|  |  |  |           {\code{delitem(\var{o}, \var{k})}} | 
					
						
							|  |  |  |   \lineiii{Indexing}{\code{\var{o}[\var{k}]}} | 
					
						
							|  |  |  |           {\code{getitem(\var{o}, \var{k})}} | 
					
						
							|  |  |  |   \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}} | 
					
						
							|  |  |  |           {\code{lshift(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}} | 
					
						
							|  |  |  |           {\code{mod(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Multiplication}{\code{\var{a} * \var{b}}} | 
					
						
							|  |  |  |           {\code{mul(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Negation (Arithmetic)}{\code{- \var{a}}} | 
					
						
							|  |  |  |           {\code{neg(\var{a})}} | 
					
						
							|  |  |  |   \lineiii{Negation (Logical)}{\code{not \var{a}}} | 
					
						
							|  |  |  |           {\code{not_(\var{a})}} | 
					
						
							|  |  |  |   \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}} | 
					
						
							|  |  |  |           {\code{rshift(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}} | 
					
						
							|  |  |  |           {\code{repeat(\var{seq}, \var{i})}} | 
					
						
							|  |  |  |   \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}} | 
					
						
							|  |  |  |           {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}} | 
					
						
							|  |  |  |   \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}} | 
					
						
							|  |  |  |           {\code{delslice(\var{seq}, \var{i}, \var{j})}} | 
					
						
							|  |  |  |   \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}} | 
					
						
							|  |  |  |           {\code{getslice(\var{seq}, \var{i}, \var{j})}} | 
					
						
							|  |  |  |   \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}} | 
					
						
							|  |  |  |           {\code{mod(\var{s}, \var{o})}} | 
					
						
							|  |  |  |   \lineiii{Subtraction}{\code{\var{a} - \var{b}}} | 
					
						
							|  |  |  |           {\code{sub(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Truth Test}{\code{\var{o}}} | 
					
						
							|  |  |  |           {\code{truth(\var{o})}} | 
					
						
							| 
									
										
										
										
											2001-08-10 15:55:09 +00:00
										 |  |  |   \lineiii{Ordering}{\code{\var{a} < \var{b}}} | 
					
						
							|  |  |  |           {\code{lt(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Ordering}{\code{\var{a} <= \var{b}}} | 
					
						
							|  |  |  |           {\code{le(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Equality}{\code{\var{a} == \var{b}}} | 
					
						
							|  |  |  |           {\code{eq(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Difference}{\code{\var{a} != \var{b}}} | 
					
						
							|  |  |  |           {\code{ne(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Ordering}{\code{\var{a} >= \var{b}}} | 
					
						
							|  |  |  |           {\code{ge(\var{a}, \var{b})}} | 
					
						
							|  |  |  |   \lineiii{Ordering}{\code{\var{a} > \var{b}}} | 
					
						
							|  |  |  |           {\code{gt(\var{a}, \var{b})}} | 
					
						
							| 
									
										
										
										
											2000-10-22 03:19:30 +00:00
										 |  |  | \end{tableiii} |