Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93656e7

Browse files
committed
Added section describing the iterator protocol.
1 parent 6f15e57 commit 93656e7

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Doc/lib/libstdtypes.tex

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,57 @@ \subsubsection{Bit-string Operations on Integer Types \label{bitstring-ops}}
313313
\end{description}
314314

315315

316+
\subsection{Iterator Types \label{typeiter}}
317+
318+
\versionadded{2.1}
319+
\index{iterator protocol}
320+
\index{protocol!iterator}
321+
\index{sequence!iteration}
322+
\index{container!iteration over}
323+
324+
Python supports a concept of iteration over containers. This is
325+
implemented using two distinct methods; these are used to allow
326+
user-defined classes to support iteration. Sequences, described below
327+
in more detail, always support the iteration methods.
328+
329+
One method needs to be defined for container objects to provide
330+
iteration support:
331+
332+
\begin{methoddesc}[container]{__iter__}{}
333+
Return an interator object. The object is required to support the
334+
iterator protocol described below. If a container supports
335+
different types of iteration, additional methods can be provided to
336+
specifically request iterators for those iteration types. (An
337+
example of an object supporting multiple forms of iteration would be
338+
a tree structure which supports both breadth-first and depth-first
339+
traversal.) This method corresponds to the \member{tp_iter} slot of
340+
the type structure for Python objects in the Python/C API.
341+
\end{methoddesc}
342+
343+
The iterator objects themselves are required to support the following
344+
two methods, which together form the \dfn{iterator protocol}:
345+
346+
\begin{methoddesc}[iterator]{__iter__}{}
347+
Return the iterator object itself. This is required to allow both
348+
containers and iterators to be used with the \keyword{for} and
349+
\keyword{in} statements. This method corresponds to the
350+
\member{tp_iter} slot of the type structure for Python objects in
351+
the Python/C API.
352+
\end{methoddesc}
353+
354+
\begin{methoddesc}[iteratpr]{next}{}
355+
Return the next item from the container. If there are no further
356+
items, raise the \exception{StopIteration} exception. This method
357+
corresponds to the \member{tp_iternext} slot of the type structure
358+
for Python objects in the Python/C API.
359+
\end{methoddesc}
360+
361+
Python defines several iterator objects to support iteration over
362+
general and specific sequence types, dictionaries, and other more
363+
specialized forms. The specific types are not important beyond their
364+
implementation of the iterator protocol.
365+
366+
316367
\subsection{Sequence Types \label{typesseq}}
317368

318369
There are six sequence types: strings, Unicode strings, lists,

0 commit comments

Comments
 (0)