[Python-checkins] cpython (2.7): Improve itertools docs with clearer examples of pure python equivalent code.
raymond.hettinger
python-checkins at python.org
Sun Oct 30 22:53:26 CET 2011
http://hg.python.org/cpython/rev/b0065b9087ef
changeset: 73224:b0065b9087ef
branch: 2.7
parent: 73221:57f73b0f921c
user: Raymond Hettinger <python at rcn.com>
date: Sun Oct 30 14:53:17 2011 -0700
summary:
Improve itertools docs with clearer examples of pure python equivalent code.
files:
Doc/library/itertools.rst | 25 ++++++++++++++++---------
1 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -433,9 +433,9 @@
def izip(*iterables):
# izip('ABCD', 'xy') --> Ax By
- iterables = map(iter, iterables)
- while iterables:
- yield tuple(map(next, iterables))
+ iterators = map(iter, iterables)
+ while iterators:
+ yield tuple(map(next, iterators))
.. versionchanged:: 2.4
When no iterables are specified, returns a zero length iterator instead of
@@ -456,17 +456,24 @@
iterables are of uneven length, missing values are filled-in with *fillvalue*.
Iteration continues until the longest iterable is exhausted. Equivalent to::
+ class ZipExhausted(Exception):
+ pass
+
def izip_longest(*args, **kwds):
# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
fillvalue = kwds.get('fillvalue')
- def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
- yield counter() # yields the fillvalue, or raises IndexError
+ counter = [len(args) - 1]
+ def sentinel():
+ if not counter[0]:
+ raise ZipExhausted
+ counter[0] -= 1
+ yield fillvalue
fillers = repeat(fillvalue)
- iters = [chain(it, sentinel(), fillers) for it in args]
+ iterators = [chain(it, sentinel(), fillers) for it in args]
try:
- for tup in izip(*iters):
- yield tup
- except IndexError:
+ while iterators:
+ yield tuple(map(next, iterators))
+ except ZipExhausted:
pass
If one of the iterables is potentially infinite, then the
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list