summaryrefslogtreecommitdiffstats
path: root/Doc/lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-04-21 15:47:16 (GMT)
committerGeorg Brandl <georg@python.org>2007-04-21 15:47:16 (GMT)
commita18af4e7a2091d11478754eb66ae387a85535763 (patch)
treefea8015d656cfee937bb6f3d106e6ca0e9f19d78 /Doc/lib
parent4d2adcca52ced412d4bdf131b872729c43520d58 (diff)
downloadcpython-a18af4e7a2091d11478754eb66ae387a85535763.zip
cpython-a18af4e7a2091d11478754eb66ae387a85535763.tar.gz
cpython-a18af4e7a2091d11478754eb66ae387a85535763.tar.bz2
PEP 3114: rename .next() to .__next__() and add next() builtin.
Diffstat (limited to 'Doc/lib')
-rw-r--r--Doc/lib/libcollections.tex6
-rw-r--r--Doc/lib/libcsv.tex8
-rw-r--r--Doc/lib/libdis.tex8
-rw-r--r--Doc/lib/libexcs.tex4
-rw-r--r--Doc/lib/libfuncs.tex22
-rw-r--r--Doc/lib/libitertools.tex25
-rw-r--r--Doc/lib/libstdtypes.tex29
-rw-r--r--Doc/lib/sqlite3/executemany_1.py2
8 files changed, 52 insertions, 52 deletions
diff --git a/Doc/lib/libcollections.tex b/Doc/lib/libcollections.tex
index a763e31..5a07a2d 100644
--- a/Doc/lib/libcollections.tex
+++ b/Doc/lib/libcollections.tex
@@ -174,7 +174,7 @@ def roundrobin(*iterables):
while pending:
task = pending.popleft()
try:
- yield task.next()
+ yield next(task)
except StopIteration:
continue
pending.append(task)
@@ -315,12 +315,12 @@ letter.
The function \function{int()} which always returns zero is just a special
case of constant functions. A faster and more flexible way to create
-constant functions is to use \function{itertools.repeat()} which can supply
+constant functions is to use a lambda function which can supply
any constant value (not just zero):
\begin{verbatim}
>>> def constant_factory(value):
-... return itertools.repeat(value).next
+... return lambda: value
>>> d = defaultdict(constant_factory('<missing>'))
>>> d.update(name='John', action='ran')
>>> '%(name)s %(action)s to %(object)s' % d
diff --git a/Doc/lib/libcsv.tex b/Doc/lib/libcsv.tex
index b87bc9d..54fc8db 100644
--- a/Doc/lib/libcsv.tex
+++ b/Doc/lib/libcsv.tex
@@ -487,8 +487,8 @@ class UTF8Recoder:
def __iter__(self):
return self
- def next(self):
- return self.reader.next().encode("utf-8")
+ def __next__(self):
+ return next(self.reader).encode("utf-8")
class UnicodeReader:
"""
@@ -500,8 +500,8 @@ class UnicodeReader:
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
- def next(self):
- row = self.reader.next()
+ def __next__(self):
+ row = next(self.reader)
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
diff --git a/Doc/lib/libdis.tex b/Doc/lib/libdis.tex
index 4b0d63b..2d78d9a 100644
--- a/Doc/lib/libdis.tex
+++ b/Doc/lib/libdis.tex
@@ -529,10 +529,10 @@ Set byte code counter to \var{target}.
\end{opcodedesc}
\begin{opcodedesc}{FOR_ITER}{delta}
-\code{TOS} is an iterator. Call its \method{next()} method. If this
-yields a new value, push it on the stack (leaving the iterator below
-it). If the iterator indicates it is exhausted \code{TOS} is
-popped, and the byte code counter is incremented by \var{delta}.
+ \code{TOS} is an iterator. Call its \method{__next__()} method. If this
+ yields a new value, push it on the stack (leaving the iterator below it). If
+ the iterator indicates it is exhausted \code{TOS} is popped, and the byte code
+ counter is incremented by \var{delta}.
\end{opcodedesc}
%\begin{opcodedesc}{FOR_LOOP}{delta}
diff --git a/Doc/lib/libexcs.tex b/Doc/lib/libexcs.tex
index 02e99a7..d119ed9 100644
--- a/Doc/lib/libexcs.tex
+++ b/Doc/lib/libexcs.tex
@@ -265,8 +265,8 @@ Raised when an \keyword{assert} statement fails.
\end{excdesc}
\begin{excdesc}{StopIteration}
- Raised by an iterator's \method{next()} method to signal that there
- are no further values.
+ Raised by builtin \function{next()} and an iterator's \method{__next__()}
+ method to signal that there are no further values.
This is derived from \exception{Exception} rather than
\exception{StandardError}, since this is not considered an error in
its normal application.
diff --git a/Doc/lib/libfuncs.tex b/Doc/lib/libfuncs.tex
index b1d2983..b488ce4 100644
--- a/Doc/lib/libfuncs.tex
+++ b/Doc/lib/libfuncs.tex
@@ -342,14 +342,12 @@ class C:
\end{funcdesc}
\begin{funcdesc}{enumerate}{iterable}
- Return an enumerate object. \var{iterable} must be a sequence, an
- iterator, or some other object which supports iteration. The
- \method{next()} method of the iterator returned by
- \function{enumerate()} returns a tuple containing a count (from
- zero) and the corresponding value obtained from iterating over
- \var{iterable}. \function{enumerate()} is useful for obtaining an
- indexed series: \code{(0, seq[0])}, \code{(1, seq[1])}, \code{(2,
- seq[2])}, \ldots.
+ Return an enumerate object. \var{iterable} must be a sequence, an iterator, or
+ some other object which supports iteration. The \method{__next__()} method of
+ the iterator returned by \function{enumerate()} returns a tuple containing a
+ count (from zero) and the corresponding value obtained from iterating over
+ \var{iterable}. \function{enumerate()} is useful for obtaining an indexed
+ series: \code{(0, seq[0])}, \code{(1, seq[1])}, \code{(2, seq[2])}, \ldots.
\versionadded{2.3}
\end{funcdesc}
@@ -615,7 +613,7 @@ class C:
support either of those protocols, \exception{TypeError} is raised.
If the second argument, \var{sentinel}, is given, then \var{o} must
be a callable object. The iterator created in this case will call
- \var{o} with no arguments for each call to its \method{next()}
+ \var{o} with no arguments for each call to its \method{__next__()}
method; if the value returned is equal to \var{sentinel},
\exception{StopIteration} will be raised, otherwise the value will
be returned.
@@ -695,6 +693,12 @@ class C:
\versionchanged[Added support for the optional \var{key} argument]{2.5}
\end{funcdesc}
+\begin{funcdesc}{next}{iterator\optional{, default}}
+ Retrieve the next item from the \var{iterable} by calling its
+ \method{__next__()} method. If \var{default} is given, it is returned if the
+ iterator is exhausted, otherwise \exception{StopIteration} is raised.
+\end{funcdesc}
+
\begin{funcdesc}{object}{}
Return a new featureless object. \class{object} is a base
for all new style classes. It has the methods that are common
diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex
index ac6028b..a2f37d7 100644
--- a/Doc/lib/libitertools.tex
+++ b/Doc/lib/libitertools.tex
@@ -164,16 +164,16 @@ by functions or loops that truncate the stream.
self.tgtkey = self.currkey = self.currvalue = xrange(0)
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
while self.currkey == self.tgtkey:
- self.currvalue = self.it.next() # Exit on StopIteration
+ self.currvalue = next(self.it) # Exit on StopIteration
self.currkey = self.keyfunc(self.currvalue)
self.tgtkey = self.currkey
return (self.currkey, self._grouper(self.tgtkey))
def _grouper(self, tgtkey):
while self.currkey == tgtkey:
yield self.currvalue
- self.currvalue = self.it.next() # Exit on StopIteration
+ self.currvalue = next(self.it) # Exit on StopIteration
self.currkey = self.keyfunc(self.currvalue)
\end{verbatim}
\versionadded{2.4}
@@ -227,7 +227,7 @@ by functions or loops that truncate the stream.
def imap(function, *iterables):
iterables = map(iter, iterables)
while True:
- args = [i.next() for i in iterables]
+ args = [next(i) for i in iterables]
if function is None:
yield tuple(args)
else:
@@ -253,11 +253,11 @@ by functions or loops that truncate the stream.
def islice(iterable, *args):
s = slice(*args)
it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
- nexti = it.next()
+ nexti = next(it)
for i, element in enumerate(iterable):
if i == nexti:
yield element
- nexti = it.next()
+ nexti = next(it)
\end{verbatim}
If \var{start} is \code{None}, then iteration starts at zero.
@@ -276,7 +276,7 @@ by functions or loops that truncate the stream.
def izip(*iterables):
iterables = map(iter, iterables)
while iterables:
- result = [it.next() for it in iterables]
+ result = [next(it) for it in iterables]
yield tuple(result)
\end{verbatim}
@@ -297,7 +297,7 @@ by functions or loops that truncate the stream.
from each iterator in-turn, but the process ends when one of the iterators
terminates. This leaves the last fetched values in limbo (they cannot be
returned in a final, incomplete tuple and they are cannot be pushed back
- into the iterator for retrieval with \code{it.next()}). In general,
+ into the iterator for retrieval with \code{next(it)}). In general,
\function{izip()} should only be used with unequal length inputs when you
don't care about trailing, unmatched values from the longer iterables.
\end{funcdesc}
@@ -360,7 +360,7 @@ by functions or loops that truncate the stream.
def starmap(function, iterable):
iterable = iter(iterable)
while True:
- yield function(*iterable.next())
+ yield function(*next(iterable))
\end{verbatim}
\end{funcdesc}
@@ -393,7 +393,7 @@ by functions or loops that truncate the stream.
item = data.pop(i)
yield item
it = iter(iterable)
- return (gen(it.next), gen(it.next))
+ return (gen(it.__next__), gen(it.__next__))
\end{verbatim}
Note, once \function{tee()} has made a split, the original \var{iterable}
@@ -556,10 +556,7 @@ def repeatfunc(func, times=None, *args):
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
- try:
- b.next()
- except StopIteration:
- pass
+ next(b, None)
return izip(a, b)
def grouper(n, iterable, padvalue=None):
diff --git a/Doc/lib/libstdtypes.tex b/Doc/lib/libstdtypes.tex
index f3ce92a..d7b8858 100644
--- a/Doc/lib/libstdtypes.tex
+++ b/Doc/lib/libstdtypes.tex
@@ -388,18 +388,17 @@ general and specific sequence types, dictionaries, and other more
specialized forms. The specific types are not important beyond their
implementation of the iterator protocol.
-The intention of the protocol is that once an iterator's
-\method{next()} method raises \exception{StopIteration}, it will
-continue to do so on subsequent calls. Implementations that
-do not obey this property are deemed broken. (This constraint
-was added in Python 2.3; in Python 2.2, various iterators are
-broken according to this rule.)
+The intention of the protocol is that once an iterator's \method{__next__()}
+method raises \exception{StopIteration}, it will continue to do so on subsequent
+calls. Implementations that do not obey this property are deemed broken. (This
+constraint was added in Python 2.3; in Python 2.2, various iterators are broken
+according to this rule.)
-Python's generators provide a convenient way to implement the
-iterator protocol. If a container object's \method{__iter__()}
-method is implemented as a generator, it will automatically
-return an iterator object (technically, a generator object)
-supplying the \method{__iter__()} and \method{next()} methods.
+Python's generators provide a convenient way to implement the iterator protocol.
+If a container object's \method{__iter__()} method is implemented as a
+generator, it will automatically return an iterator object (technically, a
+generator object) supplying the \method{__iter__()} and \method{__next__()}
+methods.
\section{Sequence Types ---
@@ -1587,17 +1586,17 @@ finally:
with a real file, this method should \emph{not} be implemented.}
\end{methoddesc}
-\begin{methoddesc}[file]{next}{}
+\begin{methoddesc}[file]{__next__}{}
A file object is its own iterator, for example \code{iter(\var{f})} returns
\var{f} (unless \var{f} is closed). When a file is used as an
iterator, typically in a \keyword{for} loop (for example,
-\code{for line in f: print line}), the \method{next()} method is
+\code{for line in f: print line}), the \method{__next__()} method is
called repeatedly. This method returns the next input line, or raises
\exception{StopIteration} when \EOF{} is hit. In order to make a
\keyword{for} loop the most efficient way of looping over the lines of
-a file (a very common operation), the \method{next()} method uses a
+a file (a very common operation), the \method{__next__()} method uses a
hidden read-ahead buffer. As a consequence of using a read-ahead
-buffer, combining \method{next()} with other file methods (like
+buffer, combining \method{__next__()} with other file methods (like
\method{readline()}) does not work right. However, using
\method{seek()} to reposition the file to an absolute position will
flush the read-ahead buffer.
diff --git a/Doc/lib/sqlite3/executemany_1.py b/Doc/lib/sqlite3/executemany_1.py
index 24357c5..2dc72cd 100644
--- a/Doc/lib/sqlite3/executemany_1.py
+++ b/Doc/lib/sqlite3/executemany_1.py
@@ -7,7 +7,7 @@ class IterChars:
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
if self.count > ord('z'):
raise StopIteration
self.count += 1