📄 module-operator.html
字号:
Return <var>a</var> <code>+</code> <var>b</var> for <var>a</var> and <var>b</var> sequences.
</dl>
<P>
<dl><dt><b><a name='l2h-344'><tt class='function'>repeat</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__repeat__</tt></b> (<var>a, b</var>)
<dd>
Return <var>a</var> <code>*</code> <var>b</var> where <var>a</var> is a sequence and
<var>b</var> is an integer.
</dl>
<P>
<dl><dt><b><a name='l2h-345'><tt class='function'>contains</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__contains__</tt></b> (<var>a, b</var>)
<dd>
Return the outcome of the test <var>b</var> <code>in</code> <var>a</var>.
Note the reversed operands. The name <tt class="function">__contains__()</tt> was
added in Python 2.0.
</dl>
<P>
<dl><dt><b><a name='l2h-346'><tt class='function'>sequenceIncludes</tt></a></b> (<var>...</var>)
<dd>
<b>Deprecated since release 2.0.</b>
Use <tt class="function">contains()</tt> instead.<p>
Alias for <tt class="function">contains()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-347'><tt class='function'>countOf</tt></a></b> (<var>a, b</var>)
<dd>
Return the number of occurrences of <var>b</var> in <var>a</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-348'><tt class='function'>indexOf</tt></a></b> (<var>a, b</var>)
<dd>
Return the index of the first of occurrence of <var>b</var> in <var>a</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-349'><tt class='function'>getitem</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__getitem__</tt></b> (<var>a, b</var>)
<dd>
Return the value of <var>a</var> at index <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-350'><tt class='function'>setitem</tt></a></b> (<var>a, b, c</var>)
<dd>
<dt><b><tt class='function'>__setitem__</tt></b> (<var>a, b, c</var>)
<dd>
Set the value of <var>a</var> at index <var>b</var> to <var>c</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-351'><tt class='function'>delitem</tt></a></b> (<var>a, b</var>)
<dd>
<dt><b><tt class='function'>__delitem__</tt></b> (<var>a, b</var>)
<dd>
Remove the value of <var>a</var> at index <var>b</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-352'><tt class='function'>getslice</tt></a></b> (<var>a, b, c</var>)
<dd>
<dt><b><tt class='function'>__getslice__</tt></b> (<var>a, b, c</var>)
<dd>
Return the slice of <var>a</var> from index <var>b</var> to index <var>c</var><code>-1</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-353'><tt class='function'>setslice</tt></a></b> (<var>a, b, c, v</var>)
<dd>
<dt><b><tt class='function'>__setslice__</tt></b> (<var>a, b, c, v</var>)
<dd>
Set the slice of <var>a</var> from index <var>b</var> to index <var>c</var><code>-1</code> to the
sequence <var>v</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-354'><tt class='function'>delslice</tt></a></b> (<var>a, b, c</var>)
<dd>
<dt><b><tt class='function'>__delslice__</tt></b> (<var>a, b, c</var>)
<dd>
Delete the slice of <var>a</var> from index <var>b</var> to index <var>c</var><code>-1</code>.
</dl>
<P>
The <tt class="module">operator</tt> also defines a few predicates to test the type
of objects. <b>Note:</b> Be careful not to misinterpret the
results of these functions; only <tt class="function">isCallable()</tt> has any
measure of reliability with instance objects. For example:
<P>
<dl><dd><pre class="verbatim">
>>> class C:
... pass
...
>>> import operator
>>> o = C()
>>> operator.isMappingType(o)
1
</pre></dl>
<P>
<dl><dt><b><a name='l2h-355'><tt class='function'>isCallable</tt></a></b> (<var>o</var>)
<dd>
<b>Deprecated since release 2.0.</b>
Use the <tt class="function">callable()</tt> built-in function instead.<p>
Returns true if the object <var>o</var> can be called like a function,
otherwise it returns false. True is returned for functions, bound and
unbound methods, class objects, and instance objects which support the
<tt class="method">__call__()</tt> method.
</dl>
<P>
<dl><dt><b><a name='l2h-356'><tt class='function'>isMappingType</tt></a></b> (<var>o</var>)
<dd>
Returns true if the object <var>o</var> supports the mapping interface.
This is true for dictionaries and all instance objects.
<b>Warning:</b> There is no reliable way to test if an instance
supports the complete mapping protocol since the interface itself is
ill-defined. This makes this test less useful than it otherwise might
be.
</dl>
<P>
<dl><dt><b><a name='l2h-357'><tt class='function'>isNumberType</tt></a></b> (<var>o</var>)
<dd>
Returns true if the object <var>o</var> represents a number. This is true
for all numeric types implemented in C, and for all instance objects.
<b>Warning:</b> There is no reliable way to test if an instance
supports the complete numeric interface since the interface itself is
ill-defined. This makes this test less useful than it otherwise might
be.
</dl>
<P>
<dl><dt><b><a name='l2h-358'><tt class='function'>isSequenceType</tt></a></b> (<var>o</var>)
<dd>
Returns true if the object <var>o</var> supports the sequence protocol.
This returns true for all objects which define sequence methods in C,
and for all instance objects. <b>Warning:</b> There is no reliable
way to test if an instance supports the complete sequence interface
since the interface itself is ill-defined. This makes this test less
useful than it otherwise might be.
</dl>
<P>
Example: Build a dictionary that maps the ordinals from <code>0</code> to
<code>256</code> to their character equivalents.
<P>
<dl><dd><pre class="verbatim">
>>> import operator
>>> d = {}
>>> keys = range(256)
>>> vals = map(chr, keys)
>>> map(operator.setitem, [d]*len(keys), keys, vals)
</pre></dl>
<DIV CLASS="navigation"><p><hr><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>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -