Test doctests in operator docs.

This commit is contained in:
Georg Brandl 2008-03-22 21:26:44 +00:00
parent 3f04303ba4
commit 4f0f34f131
1 changed files with 12 additions and 11 deletions

View File

@ -415,7 +415,7 @@ objects.
Be careful not to misinterpret the results of these functions; only Be careful not to misinterpret the results of these functions; only
:func:`isCallable` has any measure of reliability with instance objects. :func:`isCallable` has any measure of reliability with instance objects.
For example:: For example:
>>> class C: >>> class C:
... pass ... pass
@ -479,13 +479,14 @@ objects.
useful than it otherwise might be. useful than it otherwise might be.
Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to
their character equivalents. :: their character equivalents.
>>> import operator >>> import operator
>>> d = {} >>> d = {}
>>> keys = range(256) >>> keys = range(256)
>>> vals = map(chr, keys) >>> vals = map(chr, keys)
>>> map(operator.setitem, [d]*len(keys), keys, vals) >>> map(operator.setitem, [d]*len(keys), keys, vals)
[None, None, ..., None]
.. XXX: find a better, readable, example .. XXX: find a better, readable, example
@ -533,8 +534,9 @@ expect a function argument.
The items can be any type accepted by the operand's :meth:`__getitem__` The items can be any type accepted by the operand's :meth:`__getitem__`
method. Dictionaries accept any hashable value. Lists, tuples, and method. Dictionaries accept any hashable value. Lists, tuples, and
strings accept an index or a slice:: strings accept an index or a slice:
>>> from operator import itemgetter
>>> itemgetter(1)('ABCDEFG') >>> itemgetter(1)('ABCDEFG')
'B' 'B'
>>> itemgetter(1,3,5)('ABCDEFG') >>> itemgetter(1,3,5)('ABCDEFG')
@ -548,9 +550,8 @@ expect a function argument.
Added support for multiple item extraction. Added support for multiple item extraction.
Example of using :func:`itemgetter` to retrieve specific fields from a 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)] >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
>>> getcount = itemgetter(1) >>> getcount = itemgetter(1)
>>> map(getcount, inventory) >>> map(getcount, inventory)