From 11485b4869f989299d806e30a530dd5234c8e9b6 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 4 Feb 2009 19:34:31 +0000 Subject: [PATCH] Minor doc fixes. --- Doc/glossary.rst | 2 +- Doc/library/itertools.rst | 6 +++--- Lib/test/test_itertools.py | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 0eb3111750..808993e18e 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -396,7 +396,7 @@ Glossary also :term:`immutable`. named tuple - Any tuple subclass whose indexable elements are also accessible using + Any tuple-like class whose indexable elements are also accessible using named attributes (for example, :func:`time.localtime` returns a tuple-like object where the *year* is accessible either with an index such as ``t[0]`` or with a named attribute like ``t.tm_year``). diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 871b0cb83a..6e561992e9 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -221,7 +221,7 @@ loops that truncate the stream. class groupby(object): # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B - # [(list(g)) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D + # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D def __init__(self, iterable, key=None): if key is None: key = lambda x: x @@ -614,8 +614,8 @@ which incur interpreter overhead. return imap(function, count(start)) def nth(iterable, n): - "Returns the nth item or empty list" - return list(islice(iterable, n, n+1)) + "Returns the nth item or None" + return next(islice(iterable, n, None), None) def quantify(iterable, pred=bool): "Count how many times the predicate is true" diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index a45e608170..91939e247e 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1201,8 +1201,8 @@ Samuele ... return imap(function, count(start)) >>> def nth(iterable, n): -... "Returns the nth item or empty list" -... return list(islice(iterable, n, n+1)) +... "Returns the nth item or None" +... return next(islice(iterable, n, None), None) >>> def quantify(iterable, pred=bool): ... "Count how many times the predicate is true" @@ -1318,7 +1318,10 @@ perform as purported. [0, 2, 4, 6] >>> nth('abcde', 3) -['d'] +'d' + +>>> nth('abcde', 9) is None +True >>> quantify(xrange(99), lambda x: x%2==0) 50 -- 2.49.0