1 \section{\module{operator
} ---
2 Standard operators as functions.
}
3 \declaremodule{builtin
}{operator
}
4 \sectionauthor{Skip Montanaro
}{skip@automatrix.com
}
6 \modulesynopsis{All Python's standard operators as built-in functions.
}
9 The
\module{operator
} module exports a set of functions implemented in C
10 corresponding to the intrinsic operators of Python. For example,
11 \code{operator.add(x, y)
} is equivalent to the expression
\code{x+y
}. The
12 function names are those used for special class methods; variants without
13 leading and trailing
\samp{__
} are also provided for convenience.
15 The
\module{operator
} module defines the following functions:
17 \begin{funcdesc
}{add
}{a, b
}
18 \funcline{__add__
}{a, b
}
19 Return
\var{a
} \code{+
} \var{b
}, for
\var{a
} and
\var{b
} numbers.
22 \begin{funcdesc
}{sub
}{a, b
}
23 \funcline{__sub__
}{a, b
}
24 Return
\var{a
} \code{-
} \var{b
}.
27 \begin{funcdesc
}{mul
}{a, b
}
28 \funcline{__mul__
}{a, b
}
29 Return
\var{a
} \code{*
} \var{b
}, for
\var{a
} and
\var{b
} numbers.
32 \begin{funcdesc
}{div
}{a, b
}
33 \funcline{__div__
}{a, b
}
34 Return
\var{a
} \code{/
} \var{b
}.
37 \begin{funcdesc
}{mod
}{a, b
}
38 \funcline{__mod__
}{a, b
}
39 Return
\var{a
} \code{\%
} \var{b
}.
42 \begin{funcdesc
}{neg
}{o
}
44 Return
\var{o
} negated.
47 \begin{funcdesc
}{pos
}{o
}
49 Return
\var{o
} positive.
52 \begin{funcdesc
}{abs
}{o
}
54 Return the absolute value of
\var{o
}.
57 \begin{funcdesc
}{inv
}{o
}
59 Return the inverse of
\var{o
}.
62 \begin{funcdesc
}{lshift
}{a, b
}
63 \funcline{__lshift__
}{a, b
}
64 Return
\var{a
} shifted left by
\var{b
}.
67 \begin{funcdesc
}{rshift
}{a, b
}
68 \funcline{__rshift__
}{a, b
}
69 Return
\var{a
} shifted right by
\var{b
}.
72 \begin{funcdesc
}{and_
}{a, b
}
73 \funcline{__and__
}{a, b
}
74 Return the bitwise and of
\var{a
} and
\var{b
}.
77 \begin{funcdesc
}{or_
}{a, b
}
78 \funcline{__or__
}{a, b
}
79 Return the bitwise or of
\var{a
} and
\var{b
}.
82 \begin{funcdesc
}{xor
}{a, b
}
83 \funcline{__xor__
}{a, b
}
84 Return the bitwise exclusive or of
\var{a
} and
\var{b
}.
87 \begin{funcdesc
}{not_
}{o
}
89 Return the outcome of
\keyword{not
} \var{o
}. (Note that there is no
90 \method{__not__()
} discipline for object instances; only the
91 interpreter core defines this operation.)
94 \begin{funcdesc
}{truth
}{o
}
95 Return
\code{1} if
\var{o
} is true, and
0 otherwise.
98 \begin{funcdesc
}{concat
}{a, b
}
99 \funcline{__concat__
}{a, b
}
100 Return
\var{a
} \code{+
} \var{b
} for
\var{a
} and
\var{b
} sequences.
103 \begin{funcdesc
}{repeat
}{a, b
}
104 \funcline{__repeat__
}{a, b
}
105 Return
\var{a
} \code{*
} \var{b
} where
\var{a
} is a sequence and
106 \var{b
} is an integer.
109 \begin{funcdesc
}{contains
}{a, b
}
110 \funcline{sequenceIncludes
}{a, b
}
111 Return the outcome of the test
\var{b
} \code{in
} \var{a
}.
112 Note the reversed operands.
115 \begin{funcdesc
}{countOf
}{a, b
}
116 Return the number of occurrences of
\var{b
} in
\var{a
}.
119 \begin{funcdesc
}{indexOf
}{a, b
}
120 Return the index of the first of occurrence of
\var{b
} in
\var{a
}.
123 \begin{funcdesc
}{getitem
}{a, b
}
124 \funcline{__getitem__
}{a, b
}
125 Return the value of
\var{a
} at index
\var{b
}.
128 \begin{funcdesc
}{setitem
}{a, b, c
}
129 \funcline{__setitem__
}{a, b, c
}
130 Set the value of
\var{a
} at index
\var{b
} to
\var{c
}.
133 \begin{funcdesc
}{delitem
}{a, b
}
134 \funcline{__delitem__
}{a, b
}
135 Remove the value of
\var{a
} at index
\var{b
}.
138 \begin{funcdesc
}{getslice
}{a, b, c
}
139 \funcline{__getslice__
}{a, b, c
}
140 Return the slice of
\var{a
} from index
\var{b
} to index
\var{c
}\code{-
1}.
143 \begin{funcdesc
}{setslice
}{a, b, c, v
}
144 \funcline{__setslice__
}{a, b, c, v
}
145 Set the slice of
\var{a
} from index
\var{b
} to index
\var{c
}\code{-
1} to the
149 \begin{funcdesc
}{delslice
}{a, b, c
}
150 \funcline{__delslice__
}{a, b, c
}
151 Delete the slice of
\var{a
} from index
\var{b
} to index
\var{c
}\code{-
1}.
155 Example: Build a dictionary that maps the ordinals from
\code{0} to
156 \code{256} to their character equivalents.
161 >>> keys = range(
256)
162 >>> vals = map(chr, keys)
163 >>> map(operator.setitem,
[d
]*len(keys), keys, vals)