<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"module-inspect.html" />
<link rel=
"prev" href=
"module-UserString.html" />
<link rel=
"parent" href=
"python.html" />
<link rel=
"next" href=
"operator-map.html" />
<meta name='aesop' content='information'
/>
<title>3.10 operator -- Standard operators as functions.
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.9 UserString "
href=
"module-UserString.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3. Python Runtime Services"
href=
"python.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.10.1 Mapping Operators to"
href=
"operator-map.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-UserString.html">3.9 UserString
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"python.html">3. Python Runtime Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"operator-map.html">3.10.1 Mapping Operators to
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION0051000000000000000000">
3.10 <tt class=
"module">operator
</tt> --
Standard operators as functions.
</A>
<A NAME=
"module-operator"></A>
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.
The functions fall into categories that perform object comparisons,
logical operations, mathematical operations, sequence operations, and
The object comparison functions are useful for all objects, and are
named after the rich comparison operators they support:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
490' xml:id='l2h-
490'
class=
"function">lt
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">le
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">eq
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">ne
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">ge
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">gt
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__lt__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__le__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__eq__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__ne__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__ge__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__gt__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Perform ``rich comparisons'' between
<var>a
</var> and
<var>b
</var>. Specifically,
<code>lt(
<var>a
</var>,
<var>b
</var>)
</code> is equivalent to
<code><var>a
</var> < <var>b
</var></code>,
<code>le(
<var>a
</var>,
<var>b
</var>)
</code> is equivalent to
<code><var>a
</var> <=
<var>b
</var></code>,
<code>eq(
<var>a
</var>,
<var>b
</var>)
</code> is equivalent to
<code><var>a
</var> ==
<var>b
</var></code>,
<code>ne(
<var>a
</var>,
<var>b
</var>)
</code> is equivalent to
<code><var>a
</var> !=
<var>b
</var></code>,
<code>gt(
<var>a
</var>,
<var>b
</var>)
</code> is equivalent to
<code><var>a
</var> > <var>b
</var></code>
<code>ge(
<var>a
</var>,
<var>b
</var>)
</code> is equivalent to
<code><var>a
</var> >=
<var>b
</var></code>.
Note that unlike the built-in
<tt class=
"function">cmp()
</tt>, these functions can
return any value, which may or may not be interpretable as a Boolean
value. See the
<em class=
"citetitle"><a
title=
"Python Reference Manual"
>Python Reference Manual
</a></em>
for more information about rich comparisons.
<span class=
"versionnote">New in version
2.2.
</span>
The logical operations are also generally applicable to all objects,
and support truth tests, identity tests, and boolean operations:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
502' xml:id='l2h-
502'
class=
"function">not_
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__not__
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<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. The result is affected by the
<tt class=
"method">__nonzero__()
</tt> and
<tt class=
"method">__len__()
</tt> methods.)
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
504' xml:id='l2h-
504'
class=
"function">truth
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
Return
<tt class=
"constant">True
</tt> if
<var>o
</var> is true, and
<tt class=
"constant">False
</tt>
otherwise. This is equivalent to using the
<tt class=
"class">bool
</tt>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
505' xml:id='l2h-
505'
class=
"function">is_
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
Return
<code><var>a
</var> is
<var>b
</var></code>. Tests object identity.
<span class=
"versionnote">New in version
2.3.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
506' xml:id='l2h-
506'
class=
"function">is_not
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
Return
<code><var>a
</var> is not
<var>b
</var></code>. Tests object identity.
<span class=
"versionnote">New in version
2.3.
</span>
The mathematical and bitwise operations are the most numerous:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
507' xml:id='l2h-
507'
class=
"function">abs
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__abs__
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dd>Return the absolute value of
<var>o
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
509' xml:id='l2h-
509'
class=
"function">add
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__add__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>+
</code> <var>b
</var>, for
<var>a
</var> and
<var>b
</var> numbers.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
511' xml:id='l2h-
511'
class=
"function">and_
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__and__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return the bitwise and of
<var>a
</var> and
<var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
513' xml:id='l2h-
513'
class=
"function">div
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__div__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>/
</code> <var>b
</var> when
<code>__future__.division
</code> is not
in effect. This is also known as ``classic'' division.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
515' xml:id='l2h-
515'
class=
"function">floordiv
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__floordiv__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>//
</code> <var>b
</var>.
<span class=
"versionnote">New in version
2.2.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
517' xml:id='l2h-
517'
class=
"function">inv
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">invert
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__inv__
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dd><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__invert__
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dd>Return the bitwise inverse of the number
<var>o
</var>. This is equivalent
to
<code>~
</code><var>o
</var>. The names
<tt class=
"function">invert()
</tt> and
<tt class=
"function">__invert__()
</tt> were added in Python
2.0.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
521' xml:id='l2h-
521'
class=
"function">lshift
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__lshift__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> shifted left by
<var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
523' xml:id='l2h-
523'
class=
"function">mod
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__mod__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>%
</code> <var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
525' xml:id='l2h-
525'
class=
"function">mul
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__mul__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>*
</code> <var>b
</var>, for
<var>a
</var> and
<var>b
</var> numbers.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
527' xml:id='l2h-
527'
class=
"function">neg
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__neg__
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dd>Return
<var>o
</var> negated.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
529' xml:id='l2h-
529'
class=
"function">or_
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__or__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return the bitwise or of
<var>a
</var> and
<var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
531' xml:id='l2h-
531'
class=
"function">pos
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__pos__
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<dd>Return
<var>o
</var> positive.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
533' xml:id='l2h-
533'
class=
"function">pow
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__pow__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>**
</code> <var>b
</var>, for
<var>a
</var> and
<var>b
</var> numbers.
<span class=
"versionnote">New in version
2.3.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
535' xml:id='l2h-
535'
class=
"function">rshift
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__rshift__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> shifted right by
<var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
537' xml:id='l2h-
537'
class=
"function">sub
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__sub__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>-
</code> <var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
539' xml:id='l2h-
539'
class=
"function">truediv
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__truediv__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>/
</code> <var>b
</var> when
<code>__future__.division
</code> is in
effect. This is also known as division.
<span class=
"versionnote">New in version
2.2.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
541' xml:id='l2h-
541'
class=
"function">xor
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__xor__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return the bitwise exclusive or of
<var>a
</var> and
<var>b
</var>.
Operations which work with sequences include:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
543' xml:id='l2h-
543'
class=
"function">concat
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__concat__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return
<var>a
</var> <code>+
</code> <var>b
</var> for
<var>a
</var> and
<var>b
</var> sequences.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
545' xml:id='l2h-
545'
class=
"function">contains
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__contains__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<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
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
547' xml:id='l2h-
547'
class=
"function">countOf
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
Return the number of occurrences of
<var>b
</var> in
<var>a
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
548' xml:id='l2h-
548'
class=
"function">delitem
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__delitem__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Remove the value of
<var>a
</var> at index
<var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
550' xml:id='l2h-
550'
class=
"function">delslice
</tt></b>(
</nobr></td>
<td><var>a, b, c
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__delslice__
</tt></b>(
</nobr></td>
<td><var>a, b, c
</var>)
</td></tr></table></dt>
<dd>Delete the slice of
<var>a
</var> from index
<var>b
</var> to index
<var>c
</var><code>-
1</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
552' xml:id='l2h-
552'
class=
"function">getitem
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__getitem__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dd>Return the value of
<var>a
</var> at index
<var>b
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
554' xml:id='l2h-
554'
class=
"function">getslice
</tt></b>(
</nobr></td>
<td><var>a, b, c
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__getslice__
</tt></b>(
</nobr></td>
<td><var>a, b, c
</var>)
</td></tr></table></dt>
<dd>Return the slice of
<var>a
</var> from index
<var>b
</var> to index
<var>c
</var><code>-
1</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
556' xml:id='l2h-
556'
class=
"function">indexOf
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
Return the index of the first of occurrence of
<var>b
</var> in
<var>a
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
557' xml:id='l2h-
557'
class=
"function">repeat
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__repeat__
</tt></b>(
</nobr></td>
<td><var>a, b
</var>)
</td></tr></table></dt>
<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><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
559' xml:id='l2h-
559'
class=
"function">sequenceIncludes
</tt></b>(
</nobr></td>
<td><var>...
</var>)
</td></tr></table></dt>
<div class=
"versionnote"><b>Deprecated since release
2.0.
</b>
Use
<tt class=
"function">contains()
</tt> instead.
</div><p></p>
Alias for
<tt class=
"function">contains()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
560' xml:id='l2h-
560'
class=
"function">setitem
</tt></b>(
</nobr></td>
<td><var>a, b, c
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__setitem__
</tt></b>(
</nobr></td>
<td><var>a, b, c
</var>)
</td></tr></table></dt>
<dd>Set the value of
<var>a
</var> at index
<var>b
</var> to
<var>c
</var>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
562' xml:id='l2h-
562'
class=
"function">setslice
</tt></b>(
</nobr></td>
<td><var>a, b, c, v
</var>)
</td></tr></table></dt>
<dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt class=
"function">__setslice__
</tt></b>(
</nobr></td>
<td><var>a, b, c, v
</var>)
</td></tr></table></dt>
<dd>Set the slice of
<var>a
</var> from index
<var>b
</var> to index
<var>c
</var><code>-
1</code> to the
The
<tt class=
"module">operator
</tt> module also defines a few predicates to test the
type of objects.
<span class=
"note"><b class=
"label">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:
</span>
<div class=
"verbatim"><pre>
>>> import operator
>>> operator.isMappingType(o)
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
564' xml:id='l2h-
564'
class=
"function">isCallable
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
<div class=
"versionnote"><b>Deprecated since release
2.0.
</b>
Use the
<tt class=
"function">callable()
</tt> built-in function instead.
</div><p></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><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
565' xml:id='l2h-
565'
class=
"function">isMappingType
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
Returns true if the object
<var>o
</var> supports the mapping interface.
This is true for dictionaries and all instance objects defining
<tt class=
"method">__getitem__
</tt>.
<span class=
"warning"><b class=
"label">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
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
566' xml:id='l2h-
566'
class=
"function">isNumberType
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
Returns true if the object
<var>o
</var> represents a number. This is true
for all numeric types implemented in C.
<span class=
"warning"><b class=
"label">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
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
567' xml:id='l2h-
567'
class=
"function">isSequenceType
</tt></b>(
</nobr></td>
<td><var>o
</var>)
</td></tr></table></dt>
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 defining
<tt class=
"method">__getitem__
</tt>.
<span class=
"warning"><b class=
"label">Warning:
</b>
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.
</span>
Example: Build a dictionary that maps the ordinals from
<code>0</code> to
<code>255</code> to their character equivalents.
<div class=
"verbatim"><pre>
>>> import operator
>>> keys = range(
256)
>>> vals = map(chr, keys)
>>> map(operator.setitem, [d]*len(keys), keys, vals)
The
<tt class=
"module">operator
</tt> module also defines tools for generalized attribute
and item lookups. These are useful for making fast field extractors
as arguments for
<tt class=
"function">map()
</tt>,
<tt class=
"function">sorted()
</tt>,
<tt class=
"method">itertools.groupby()
</tt>, or other functions that expect a
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
568' xml:id='l2h-
568'
class=
"function">attrgetter
</tt></b>(
</nobr></td>
<td><var>attr
</var>)
</td></tr></table></dt>
Return a callable object that fetches
<var>attr
</var> from its operand.
After,
"<tt class="samp
">f=attrgetter('name')</tt>", the call
"<tt class="samp
">f(b)</tt>" returns
"<tt class="samp
">b.name</tt>".
<span class=
"versionnote">New in version
2.4.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
569' xml:id='l2h-
569'
class=
"function">itemgetter
</tt></b>(
</nobr></td>
<td><var>item
</var>)
</td></tr></table></dt>
Return a callable object that fetches
<var>item
</var> from its operand.
After,
"<tt class="samp
">f=itemgetter(2)</tt>", the call
"<tt class="samp
">f(b)</tt>" returns
"<tt class="samp
">b[2]</tt>".
<span class=
"versionnote">New in version
2.4.
</span>
<div class=
"verbatim"><pre>
>>> from operator import *
>>> inventory = [('apple',
3), ('banana',
2), ('pear',
5), ('orange',
1)]
>>> getcount = itemgetter(
1)
>>> map(getcount, inventory)
>>> sorted(inventory, key=getcount)
[('orange',
1), ('banana',
2), ('apple',
3), ('pear',
5)]
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"operator-map.html">3.10.1 Mapping Operators to Functions
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.9 UserString "
href=
"module-UserString.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3. Python Runtime Services"
href=
"python.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.10.1 Mapping Operators to"
href=
"operator-map.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-UserString.html">3.9 UserString
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"python.html">3. Python Runtime Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"operator-map.html">3.10.1 Mapping Operators to
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.