summaryrefslogtreecommitdiffstats
path: root/Doc/library/operator.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/operator.rst')
-rw-r--r--Doc/library/operator.rst29
1 files changed, 16 insertions, 13 deletions
diff --git a/Doc/library/operator.rst b/Doc/library/operator.rst
index 590098b..047c7ec 100644
--- a/Doc/library/operator.rst
+++ b/Doc/library/operator.rst
@@ -6,6 +6,11 @@
.. sectionauthor:: Skip Montanaro <skip@automatrix.com>
+.. testsetup::
+
+ import operator
+ from operator import itemgetter
+
The :mod:`operator` module exports a set of functions implemented in C
corresponding to the intrinsic operators of Python. For example,
@@ -346,7 +351,7 @@ objects.
Be careful not to misinterpret the results of these functions; none have any
measure of reliability with instance objects.
- For example::
+ For example:
>>> class C:
... pass
@@ -399,13 +404,12 @@ objects.
useful than it otherwise might be.
Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to
-their character equivalents. ::
+their character equivalents.
- >>> import operator
>>> d = {}
>>> keys = range(256)
>>> vals = map(chr, keys)
- >>> map(operator.setitem, [d]*len(keys), keys, vals)
+ >>> map(operator.setitem, [d]*len(keys), keys, vals) # doctest: +SKIP
.. XXX: find a better, readable, example
@@ -444,21 +448,20 @@ expect a function argument.
The items can be any type accepted by the operand's :meth:`__getitem__`
method. Dictionaries accept any hashable value. Lists, tuples, and
- strings accept an index or a slice::
+ strings accept an index or a slice:
- >>> itemgetter(1)('ABCDEFG')
- 'B'
- >>> itemgetter(1,3,5)('ABCDEFG')
- ('B', 'D', 'F')
- >>> itemgetter(slice(2,None))('ABCDEFG')
- 'CDEFG'
+ >>> itemgetter(1)('ABCDEFG')
+ 'B'
+ >>> itemgetter(1,3,5)('ABCDEFG')
+ ('B', 'D', 'F')
+ >>> itemgetter(slice(2,None))('ABCDEFG')
+ 'CDEFG'
.. versionadded:: 2.4
Example of using :func:`itemgetter` to retrieve specific fields from a
- tuple record::
+ tuple record:
- >>> from operator import itemgetter
>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
>>> getcount = itemgetter(1)
>>> map(getcount, inventory)