diff options
author | Raymond Hettinger <python@rcn.com> | 2013-03-31 06:37:57 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2013-03-31 06:37:57 (GMT) |
commit | 56bb8b9bdc2626ae6d5097cd4ef6aac6d2ab015f (patch) | |
tree | f7e544bda1b6f674aa07c8ab1f7678a99607bd96 | |
parent | 6e4e4abf16ca1de9ace308be25186335b426b35d (diff) | |
download | cpython-56bb8b9bdc2626ae6d5097cd4ef6aac6d2ab015f.zip cpython-56bb8b9bdc2626ae6d5097cd4ef6aac6d2ab015f.tar.gz cpython-56bb8b9bdc2626ae6d5097cd4ef6aac6d2ab015f.tar.bz2 |
Add an itertools recipe showing how to use t.__copy__().
-rw-r--r-- | Doc/library/itertools.rst | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index a7f0058..0e98c1e 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -828,6 +828,18 @@ which incur interpreter overhead. indices = sorted(random.randrange(n) for i in xrange(r)) return tuple(pool[i] for i in indices) + def tee_lookahead(t, i): + """Inspect the i-th upcomping value from a tee object + while leaving the tee object at its current position. + + Raise an IndexError if the underlying iterator doesn't + have enough values. + + """ + for value in islice(t.__copy__(), i, None): + return value + raise IndexError(i) + Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the *dotproduct* recipe can be written as:: |