]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add an itertools recipe showing how to use t.__copy__().
authorRaymond Hettinger <python@rcn.com>
Sun, 31 Mar 2013 06:37:57 +0000 (23:37 -0700)
committerRaymond Hettinger <python@rcn.com>
Sun, 31 Mar 2013 06:37:57 +0000 (23:37 -0700)
Doc/library/itertools.rst

index a7f0058b4cd157735536bc2bfff19c42c17179c0..0e98c1e2adf469054abaf96a23430606e1e165db 100644 (file)
@@ -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::