The operator Module
The operator
module supplies functions that are
equivalent to Python’s operators. These functions
are handy for use with map
and
reduce
, and in other cases where callables must be
stored, passed as arguments, or returned as function results. The
functions in operator
have the same names as the
corresponding special methods (covered in Chapter 5). Each function is available with two names,
with and without the leading and trailing double underscores (e.g.,
both
operator.add(
a
,b
)
and operator.__add__(
a
,b
)
return
a
+
b
).
Table 15-1 lists the functions supplied by
operator
.
Table 15-1. Functions supplied by operator
Method |
Signature |
Behaves like |
---|---|---|
abs |
abs(
|
|
add |
add( |
|
and_ |
and_( |
|
concat |
concat( |
|
contains |
contains( |
|
countOf |
countOf( |
|
delitem |
delitem( |
|
delslice |
delslice( |
|
div |
div( |
|
getitem |
getitem( |
|
getslice |
getslice( |
|
indexOf |
indexOf( |
|
invert, inv |
|
|
lshift |
lshift( |
|
mod |
mod( |
|
mul |
mul( |
|
neg |
neg(
|
|
not_ |
not_(
|
|
or_ |
or_( |
|
pos |
pos(
|
|
repeat |
repeat( |
|
rshift |
rshift( |
|
setitem |
setitem( |
|
setslice |
setslice( |
|
sub |
sub( |
|
Get Python in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.