Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
93656e76
Commit
93656e76
authored
May 02, 2001
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added section describing the iterator protocol.
parent
6f15e579
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
Doc/lib/libstdtypes.tex
Doc/lib/libstdtypes.tex
+51
-0
No files found.
Doc/lib/libstdtypes.tex
View file @
93656e76
...
...
@@ -313,6 +313,57 @@ division by \code{pow(2, \var{n})} without overflow check.
\end{description}
\subsection
{
Iterator Types
\label
{
typeiter
}}
\versionadded
{
2.1
}
\index
{
iterator protocol
}
\index
{
protocol!iterator
}
\index
{
sequence!iteration
}
\index
{
container!iteration over
}
Python supports a concept of iteration over containers. This is
implemented using two distinct methods; these are used to allow
user-defined classes to support iteration. Sequences, described below
in more detail, always support the iteration methods.
One method needs to be defined for container objects to provide
iteration support:
\begin{methoddesc}
[container]
{__
iter
__}{}
Return an interator object. The object is required to support the
iterator protocol described below. If a container supports
different types of iteration, additional methods can be provided to
specifically request iterators for those iteration types. (An
example of an object supporting multiple forms of iteration would be
a tree structure which supports both breadth-first and depth-first
traversal.) This method corresponds to the
\member
{
tp
_
iter
}
slot of
the type structure for Python objects in the Python/C API.
\end{methoddesc}
The iterator objects themselves are required to support the following
two methods, which together form the
\dfn
{
iterator protocol
}
:
\begin{methoddesc}
[iterator]
{__
iter
__}{}
Return the iterator object itself. This is required to allow both
containers and iterators to be used with the
\keyword
{
for
}
and
\keyword
{
in
}
statements. This method corresponds to the
\member
{
tp
_
iter
}
slot of the type structure for Python objects in
the Python/C API.
\end{methoddesc}
\begin{methoddesc}
[iteratpr]
{
next
}{}
Return the next item from the container. If there are no further
items, raise the
\exception
{
StopIteration
}
exception. This method
corresponds to the
\member
{
tp
_
iternext
}
slot of the type structure
for Python objects in the Python/C API.
\end{methoddesc}
Python defines several iterator objects to support iteration over
general and specific sequence types, dictionaries, and other more
specialized forms. The specific types are not important beyond their
implementation of the iterator protocol.
\subsection
{
Sequence Types
\label
{
typesseq
}}
There are six sequence types: strings, Unicode strings, lists,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment