Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{operator} --- |
| 2 | Standard operators as functions.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{builtin}{operator} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 4 | \sectionauthor{Skip Montanaro}{skip@automatrix.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | |
| 6 | \modulesynopsis{All Python's standard operators as built-in functions.} |
| 7 | |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 8 | |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 9 | The \module{operator} module exports a set of functions implemented in C |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 10 | corresponding to the intrinsic operators of Python. For example, |
Fred Drake | f3e6df1 | 1997-12-29 17:11:55 +0000 | [diff] [blame] | 11 | \code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 12 | function names are those used for special class methods; variants without |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 13 | leading and trailing \samp{__} are also provided for convenience. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 14 | |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 15 | The \module{operator} module defines the following functions: |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 16 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 17 | \begin{funcdesc}{add}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 18 | \funcline{__add__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 19 | Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 20 | \end{funcdesc} |
| 21 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 22 | \begin{funcdesc}{sub}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 23 | \funcline{__sub__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 24 | Return \var{a} \code{-} \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 25 | \end{funcdesc} |
| 26 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 27 | \begin{funcdesc}{mul}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 28 | \funcline{__mul__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 29 | Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 30 | \end{funcdesc} |
| 31 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 32 | \begin{funcdesc}{div}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 33 | \funcline{__div__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 34 | Return \var{a} \code{/} \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 35 | \end{funcdesc} |
| 36 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 37 | \begin{funcdesc}{mod}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 38 | \funcline{__mod__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 39 | Return \var{a} \code{\%} \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{neg}{o} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 43 | \funcline{__neg__}{o} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 44 | Return \var{o} negated. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 45 | \end{funcdesc} |
| 46 | |
| 47 | \begin{funcdesc}{pos}{o} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 48 | \funcline{__pos__}{o} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 49 | Return \var{o} positive. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 50 | \end{funcdesc} |
| 51 | |
| 52 | \begin{funcdesc}{abs}{o} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 53 | \funcline{__abs__}{o} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 54 | Return the absolute value of \var{o}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 55 | \end{funcdesc} |
| 56 | |
| 57 | \begin{funcdesc}{inv}{o} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 58 | \funcline{__inv__}{o} |
Fred Drake | 5316ef4 | 2000-09-17 16:10:25 +0000 | [diff] [blame] | 59 | \funcline{__invert__}{o} |
| 60 | Return the inverse of \var{o}. The names \function{invert()} and |
| 61 | \function{__invert__()} were added in Python 2.0. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 62 | \end{funcdesc} |
| 63 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{lshift}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 65 | \funcline{__lshift__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 66 | Return \var{a} shifted left by \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 67 | \end{funcdesc} |
| 68 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 69 | \begin{funcdesc}{rshift}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 70 | \funcline{__rshift__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 71 | Return \var{a} shifted right by \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 72 | \end{funcdesc} |
| 73 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 74 | \begin{funcdesc}{and_}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 75 | \funcline{__and__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 76 | Return the bitwise and of \var{a} and \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 77 | \end{funcdesc} |
| 78 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 79 | \begin{funcdesc}{or_}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 80 | \funcline{__or__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 81 | Return the bitwise or of \var{a} and \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 82 | \end{funcdesc} |
| 83 | |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 84 | \begin{funcdesc}{xor}{a, b} |
| 85 | \funcline{__xor__}{a, b} |
| 86 | Return the bitwise exclusive or of \var{a} and \var{b}. |
| 87 | \end{funcdesc} |
| 88 | |
| 89 | \begin{funcdesc}{not_}{o} |
| 90 | \funcline{__not__}{o} |
Fred Drake | e55702b | 1999-06-15 20:56:40 +0000 | [diff] [blame] | 91 | Return the outcome of \keyword{not} \var{o}. (Note that there is no |
| 92 | \method{__not__()} discipline for object instances; only the |
| 93 | interpreter core defines this operation.) |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 94 | \end{funcdesc} |
| 95 | |
| 96 | \begin{funcdesc}{truth}{o} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 97 | Return \code{1} if \var{o} is true, and 0 otherwise. |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 98 | \end{funcdesc} |
| 99 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 100 | \begin{funcdesc}{concat}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 101 | \funcline{__concat__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 102 | Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 105 | \begin{funcdesc}{repeat}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 106 | \funcline{__repeat__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 107 | Return \var{a} \code{*} \var{b} where \var{a} is a sequence and |
| 108 | \var{b} is an integer. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 109 | \end{funcdesc} |
| 110 | |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 111 | \begin{funcdesc}{contains}{a, b} |
Fred Drake | 5316ef4 | 2000-09-17 16:10:25 +0000 | [diff] [blame] | 112 | \funcline{__contains__}{a, b} |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 113 | Return the outcome of the test \var{b} \code{in} \var{a}. |
Fred Drake | 5316ef4 | 2000-09-17 16:10:25 +0000 | [diff] [blame] | 114 | Note the reversed operands. The name \function{__contains__()} was |
| 115 | added in Python 2.0. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{sequenceIncludes}{\unspecified} |
| 119 | \deprecated{2.0}{Use \function{contains()} instead.} |
| 120 | Alias for \function{contains()}. |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 121 | \end{funcdesc} |
| 122 | |
| 123 | \begin{funcdesc}{countOf}{a, b} |
| 124 | Return the number of occurrences of \var{b} in \var{a}. |
| 125 | \end{funcdesc} |
| 126 | |
| 127 | \begin{funcdesc}{indexOf}{a, b} |
| 128 | Return the index of the first of occurrence of \var{b} in \var{a}. |
| 129 | \end{funcdesc} |
| 130 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 131 | \begin{funcdesc}{getitem}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 132 | \funcline{__getitem__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 133 | Return the value of \var{a} at index \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 134 | \end{funcdesc} |
| 135 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 136 | \begin{funcdesc}{setitem}{a, b, c} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 137 | \funcline{__setitem__}{a, b, c} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 138 | Set the value of \var{a} at index \var{b} to \var{c}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 139 | \end{funcdesc} |
| 140 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 141 | \begin{funcdesc}{delitem}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 142 | \funcline{__delitem__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 143 | Remove the value of \var{a} at index \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 144 | \end{funcdesc} |
| 145 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 146 | \begin{funcdesc}{getslice}{a, b, c} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 147 | \funcline{__getslice__}{a, b, c} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 148 | Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 149 | \end{funcdesc} |
| 150 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 151 | \begin{funcdesc}{setslice}{a, b, c, v} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 152 | \funcline{__setslice__}{a, b, c, v} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 153 | Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the |
| 154 | sequence \var{v}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 155 | \end{funcdesc} |
| 156 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 157 | \begin{funcdesc}{delslice}{a, b, c} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 158 | \funcline{__delslice__}{a, b, c} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 159 | Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 160 | \end{funcdesc} |
| 161 | |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame^] | 162 | The \module{operator} also defines a few predicates to test the type |
| 163 | of objects. \strong{Note:} Be careful not to misinterpret the |
| 164 | results of these functions; only \function{isCallable()} has any |
| 165 | measure of reliability with instance objects. For example: |
| 166 | |
| 167 | \begin{verbatim} |
| 168 | >>> class C: |
| 169 | ... pass |
| 170 | ... |
| 171 | >>> import operator |
| 172 | >>> o = C() |
| 173 | >>> operator.isMappingType(o) |
| 174 | 1 |
| 175 | \end{verbatim} |
| 176 | |
| 177 | \begin{funcdesc}{isCallable}{o} |
| 178 | \deprecated{2.0}{Use the \function{callable()} built-in function instead.} |
| 179 | Returns true if the object \var{o} can be called like a function, |
| 180 | otherwise it returns false. True is returned for functions, bound and |
| 181 | unbound methods, class objects, and instance objects which support the |
| 182 | \method{__call__()} method. |
| 183 | \end{funcdesc} |
| 184 | |
| 185 | \begin{funcdesc}{isMappingType}{o} |
| 186 | Returns true if the object \var{o} supports the mapping interface. |
| 187 | This is true for dictionaries and all instance objects. |
| 188 | \strong{Warning:} There is no reliable way to test if an instance |
| 189 | supports the complete mapping protocol since the interface itself is |
| 190 | ill-defined. This makes this test less useful than it otherwise might |
| 191 | be. |
| 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{isNumberType}{o} |
| 195 | Returns true if the object \var{o} represents a number. This is true |
| 196 | for all numeric types implemented in C, and for all instance objects. |
| 197 | \strong{Warning:} There is no reliable way to test if an instance |
| 198 | supports the complete numeric interface since the interface itself is |
| 199 | ill-defined. This makes this test less useful than it otherwise might |
| 200 | be. |
| 201 | \end{funcdesc} |
| 202 | |
| 203 | \begin{funcdesc}{isSequenceType}{o} |
| 204 | Returns true if the object \var{o} supports the sequence protocol. |
| 205 | This returns true for all objects which define sequence methods in C, |
| 206 | and for all instance objects. \strong{Warning:} There is no reliable |
| 207 | way to test if an instance supports the complete sequence interface |
| 208 | since the interface itself is ill-defined. This makes this test less |
| 209 | useful than it otherwise might be. |
| 210 | \end{funcdesc} |
| 211 | |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 212 | |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 213 | Example: Build a dictionary that maps the ordinals from \code{0} to |
| 214 | \code{256} to their character equivalents. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 215 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 216 | \begin{verbatim} |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 217 | >>> import operator |
| 218 | >>> d = {} |
| 219 | >>> keys = range(256) |
| 220 | >>> vals = map(chr, keys) |
| 221 | >>> map(operator.setitem, [d]*len(keys), keys, vals) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 222 | \end{verbatim} |