📄 module-operator.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.8 operator -- Standard operators as functions.</title>
<META NAME="description" CONTENT="3.8 operator -- Standard operators as functions.">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-traceback.html" tppabs="http://www.python.org/doc/current/lib/module-traceback.html">
<LINK REL="previous" href="module-UserString.html" tppabs="http://www.python.org/doc/current/lib/module-UserString.html">
<LINK REL="up" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
<LINK REL="next" href="module-traceback.html" tppabs="http://www.python.org/doc/current/lib/module-traceback.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-UserString.html" tppabs="http://www.python.org/doc/current/lib/module-UserString.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-traceback.html" tppabs="http://www.python.org/doc/current/lib/module-traceback.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-UserString.html" tppabs="http://www.python.org/doc/current/lib/module-UserString.html">3.7 UserString </A>
<b class="navlabel">Up:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-traceback.html" tppabs="http://www.python.org/doc/current/lib/module-traceback.html">3.9 traceback </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION005800000000000000000">
3.8 <tt class="module">operator</tt> --
Standard operators as functions.</A>
</H1>
<P>
<P>
The <tt class="module">operator</tt> module exports a set of functions implemented in C
corresponding to the intrinsic operators of Python. For example,
<code>operator.add(x, y)</code> is equivalent to the expression <code>x+y</code>. The
function names are those used for special class methods; variants without
leading and trailing "<tt class="samp">__</tt>" are also provided for convenience.
<P>
The <tt class="module">operator</tt> module defines the following functions:
<P>
<dl><dt><b><a name='l2h-327'><tt class='function'>add</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__add__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> <code>+</code> <var>b</var>, for <var>a</var> and <var>b</var> numbers.
</dl>
<P>
<dl><dt><b><a name='l2h-328'><tt class='function'>sub</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__sub__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> <code>-</code> <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-329'><tt class='function'>mul</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__mul__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> <code>*</code> <var>b</var>, for <var>a</var> and <var>b</var> numbers.
</dl>
<P>
<dl><dt><b><a name='l2h-330'><tt class='function'>div</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__div__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> <code>/</code> <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-331'><tt class='function'>mod</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__mod__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> <code>%</code> <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-332'><tt class='function'>neg</tt></a></b> (<var>o</var>)
<dd>
<dt><b><tt class='function'>__neg__</tt></b> (<var>o</var>)
<dd>
Return <var>o</var> negated.
</dl>
<P>
<dl><dt><b><a name='l2h-333'><tt class='function'>pos</tt></a></b> (<var>o</var>)
<dd>
<dt><b><tt class='function'>__pos__</tt></b> (<var>o</var>)
<dd>
Return <var>o</var> positive.
</dl>
<P>
<dl><dt><b><a name='l2h-334'><tt class='function'>abs</tt></a></b> (<var>o</var>)
<dd>
<dt><b><tt class='function'>__abs__</tt></b> (<var>o</var>)
<dd>
Return the absolute value of <var>o</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-335'><tt class='function'>inv</tt></a></b> (<var>o</var>)
<dd>
<dt><b><tt class='function'>__inv__</tt></b> (<var>o</var>)
<dd>
<dt><b><tt class='function'>__invert__</tt></b> (<var>o</var>)
<dd>
Return the inverse of <var>o</var>. The names <tt class="function">invert()</tt> and
<tt class="function">__invert__()</tt> were added in Python 2.0.
</dl>
<P>
<dl><dt><b><a name='l2h-336'><tt class='function'>lshift</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__lshift__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> shifted left by <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-337'><tt class='function'>rshift</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__rshift__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> shifted right by <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-338'><tt class='function'>and_</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__and__</tt></b> (<var>a, b</var>)
<dd>
Return the bitwise and of <var>a</var> and <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-339'><tt class='function'>or_</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__or__</tt></b> (<var>a, b</var>)
<dd>
Return the bitwise or of <var>a</var> and <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-340'><tt class='function'>xor</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__xor__</tt></b> (<var>a, b</var>)
<dd>
Return the bitwise exclusive or of <var>a</var> and <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-341'><tt class='function'>not_</tt></a></b> (<var>o</var>)
<dd>
<dt><b><tt class='function'>__not__</tt></b> (<var>o</var>)
<dd>
Return the outcome of <tt class="keyword">not</tt> <var>o</var>. (Note that there is no
<tt class="method">__not__()</tt> method for object instances; only the interpreter
core defines this operation.)
</dl>
<P>
<dl><dt><b><a name='l2h-342'><tt class='function'>truth</tt></a></b> (<var>o</var>)
<dd>
Return <code>1</code> if <var>o</var> is true, and 0 otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-343'><tt class='function'>concat</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__concat__</tt></b> (<var>a, b</var>)
<dd>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -