1 % Contributed by Skip Montanaro, from the module's doc strings.
3 \section{Built-in Module
\module{operator
}}
4 \label{module-operator
}
7 The
\module{operator
} module exports a set of functions implemented in C
8 corresponding to the intrinsic operators of Python. For example,
9 \code{operator.add(x, y)
} is equivalent to the expression
\code{x+y
}. The
10 function names are those used for special class methods; variants without
11 leading and trailing
\samp{__
} are also provided for convenience.
13 The
\module{operator
} module defines the following functions:
15 \begin{funcdesc
}{add
}{a, b
}
16 \funcline{__add__
}{a, b
}
17 Return
\var{a
} \code{+
} \var{b
}, for
\var{a
} and
\var{b
} numbers.
20 \begin{funcdesc
}{sub
}{a, b
}
21 \funcline{__sub__
}{a, b
}
22 Return
\var{a
} \code{-
} \var{b
}.
25 \begin{funcdesc
}{mul
}{a, b
}
26 \funcline{__mul__
}{a, b
}
27 Return
\var{a
} \code{*
} \var{b
}, for
\var{a
} and
\var{b
} numbers.
30 \begin{funcdesc
}{div
}{a, b
}
31 \funcline{__div__
}{a, b
}
32 Return
\var{a
} \code{/
} \var{b
}.
35 \begin{funcdesc
}{mod
}{a, b
}
36 \funcline{__mod__
}{a, b
}
37 Return
\var{a
} \code{\%
} \var{b
}.
40 \begin{funcdesc
}{neg
}{o
}
42 Return
\var{o
} negated.
45 \begin{funcdesc
}{pos
}{o
}
47 Return
\var{o
} positive.
50 \begin{funcdesc
}{abs
}{o
}
52 Return the absolute value of
\var{o
}.
55 \begin{funcdesc
}{inv
}{o
}
57 Return the inverse of
\var{o
}.
60 \begin{funcdesc
}{lshift
}{a, b
}
61 \funcline{__lshift__
}{a, b
}
62 Return
\var{a
} shifted left by
\var{b
}.
65 \begin{funcdesc
}{rshift
}{a, b
}
66 \funcline{__rshift__
}{a, b
}
67 Return
\var{a
} shifted right by
\var{b
}.
70 \begin{funcdesc
}{and_
}{a, b
}
71 \funcline{__and__
}{a, b
}
72 Return the bitwise and of
\var{a
} and
\var{b
}.
75 \begin{funcdesc
}{or_
}{a, b
}
76 \funcline{__or__
}{a, b
}
77 Return the bitwise or of
\var{a
} and
\var{b
}.
80 \begin{funcdesc
}{concat
}{a, b
}
81 \funcline{__concat__
}{a, b
}
82 Return
\var{a
} \code{+
} \var{b
} for
\var{a
} and
\var{b
} sequences.
85 \begin{funcdesc
}{repeat
}{a, b
}
86 \funcline{__repeat__
}{a, b
}
87 Return
\var{a
} \code{*
} \var{b
} where
\var{a
} is a sequence and
88 \var{b
} is an integer.
91 \begin{funcdesc
}{getitem
}{a, b
}
92 \funcline{__getitem__
}{a, b
}
93 Return the value of
\var{a
} at index
\var{b
}.
96 \begin{funcdesc
}{setitem
}{a, b, c
}
97 \funcline{__setitem__
}{a, b, c
}
98 Set the value of
\var{a
} at index
\var{b
} to
\var{c
}.
101 \begin{funcdesc
}{delitem
}{a, b
}
102 \funcline{__delitem__
}{a, b
}
103 Remove the value of
\var{a
} at index
\var{b
}.
106 \begin{funcdesc
}{getslice
}{a, b, c
}
107 \funcline{__getslice__
}{a, b, c
}
108 Return the slice of
\var{a
} from index
\var{b
} to index
\var{c
}\code{-
1}.
111 \begin{funcdesc
}{setslice
}{a, b, c, v
}
112 \funcline{__setslice__
}{a, b, c, v
}
113 Set the slice of
\var{a
} from index
\var{b
} to index
\var{c
}\code{-
1} to the
117 \begin{funcdesc
}{delslice
}{a, b, c
}
118 \funcline{__delslice__
}{a, b, c
}
119 Delete the slice of
\var{a
} from index
\var{b
} to index
\var{c
}\code{-
1}.
123 Example: Build a dictionary that maps the ordinals from
\code{0} to
124 \code{256} to their character equivalents.
129 >>> keys = range(
256)
130 >>> vals = map(chr, keys)
131 >>> map(operator.setitem,
[d
]*len(keys), keys, vals)