mirror of
https://github.com/python/cpython.git
synced 2025-12-08 02:08:20 +00:00
Give the enumerate() PEP a section of its own
Add some credits Fill in a link
This commit is contained in:
parent
007c04a9d3
commit
fad2f59313
1 changed files with 50 additions and 36 deletions
|
|
@ -215,10 +215,45 @@ and implemented by Jack Jansen.}
|
||||||
|
|
||||||
\end{seealso}
|
\end{seealso}
|
||||||
|
|
||||||
|
|
||||||
|
%======================================================================
|
||||||
|
\section{PEP 279: The \function{enumerate()} Built-in Function}
|
||||||
|
|
||||||
|
A new built-in function, \function{enumerate()}, will make
|
||||||
|
certain loops a bit clearer. \code{enumerate(thing)}, where
|
||||||
|
\var{thing} is either an iterator or a sequence, returns a iterator
|
||||||
|
that will return \code{(0, \var{thing[0]})}, \code{(1,
|
||||||
|
\var{thing[1]})}, \code{(2, \var{thing[2]})}, and so forth. Fairly
|
||||||
|
often you'll see code to change every element of a list that looks
|
||||||
|
like this:
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
for i in range(len(L)):
|
||||||
|
item = L[i]
|
||||||
|
# ... compute some result based on item ...
|
||||||
|
L[i] = result
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
This can be rewritten using \function{enumerate()} as:
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
for i, item in enumerate(L):
|
||||||
|
# ... compute some result based on item ...
|
||||||
|
L[i] = result
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{seealso}
|
||||||
|
|
||||||
|
\seepep{279}{The enumerate() built-in function}{Written
|
||||||
|
by Raymond D. Hettinger.}
|
||||||
|
|
||||||
|
\end{seealso}
|
||||||
|
|
||||||
|
|
||||||
%======================================================================
|
%======================================================================
|
||||||
\section{PEP 285: The \class{bool} Type\label{section-bool}}
|
\section{PEP 285: The \class{bool} Type\label{section-bool}}
|
||||||
|
|
||||||
|
|
||||||
A Boolean type was added to Python 2.3. Two new constants were added
|
A Boolean type was added to Python 2.3. Two new constants were added
|
||||||
to the \module{__builtin__} module, \constant{True} and
|
to the \module{__builtin__} module, \constant{True} and
|
||||||
\constant{False}. The type object for this new type is named
|
\constant{False}. The type object for this new type is named
|
||||||
|
|
@ -292,41 +327,20 @@ strings \samp{True} and \samp{False} instead of \samp{1} and \samp{0}.
|
||||||
|
|
||||||
|
|
||||||
%======================================================================
|
%======================================================================
|
||||||
\section{Other Language Changes}
|
%\section{Other Language Changes}
|
||||||
|
|
||||||
Here are the changes that Python 2.3 makes to the core language.
|
%Here are the changes that Python 2.3 makes to the core language.
|
||||||
|
|
||||||
\begin{itemize}
|
%\begin{itemize}
|
||||||
\item The \keyword{yield} statement is now always a keyword, as
|
%\item The \keyword{yield} statement is now always a keyword, as
|
||||||
described in section~\ref{section-generators}.
|
%described in section~\ref{section-generators}.
|
||||||
|
|
||||||
\item Two new constants, \constant{True} and \constant{False} were
|
%\item Two new constants, \constant{True} and \constant{False} were
|
||||||
added along with the built-in \class{bool} type, as described in
|
%added along with the built-in \class{bool} type, as described in
|
||||||
section~\ref{section-bool}.
|
%section~\ref{section-bool}.
|
||||||
|
|
||||||
\item A new built-in function, \function{enumerate()}, will make
|
%\item
|
||||||
certain loops a bit clearer. \code{enumerate(thing)}, where
|
%\end{itemize}
|
||||||
\var{thing} is either an iterator or a sequence, returns a iterator
|
|
||||||
that will return \code{(0, \var{thing[0]})}, \code{(1,
|
|
||||||
\var{thing[1]})}, \code{(2, \var{thing[2]})}, and so forth. Fairly
|
|
||||||
often you'll see code to change every element of a list that looks like this:
|
|
||||||
|
|
||||||
\begin{verbatim}
|
|
||||||
for i in range(len(L)):
|
|
||||||
item = L[i]
|
|
||||||
# ... compute some result based on item ...
|
|
||||||
L[i] = result
|
|
||||||
\end{verbatim}
|
|
||||||
|
|
||||||
This can be rewritten using \function{enumerate()} as:
|
|
||||||
|
|
||||||
\begin{verbatim}
|
|
||||||
for i, item in enumerate(L):
|
|
||||||
# ... compute some result based on item ...
|
|
||||||
L[i] = result
|
|
||||||
\end{verbatim}
|
|
||||||
|
|
||||||
\end{itemize}
|
|
||||||
|
|
||||||
|
|
||||||
%======================================================================
|
%======================================================================
|
||||||
|
|
@ -386,7 +400,7 @@ support, turn on the Python interpreter's debugging code by running
|
||||||
|
|
||||||
\begin{seealso}
|
\begin{seealso}
|
||||||
|
|
||||||
\seeurl{XXX}
|
\seeurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Objects/obmalloc.c}
|
||||||
{For the full details of the pymalloc implementation, see
|
{For the full details of the pymalloc implementation, see
|
||||||
the comments at the top of the file \file{Objects/obmalloc.c} in the
|
the comments at the top of the file \file{Objects/obmalloc.c} in the
|
||||||
Python source code. The above link points to the file within the
|
Python source code. The above link points to the file within the
|
||||||
|
|
@ -491,7 +505,7 @@ packages for use on HP-UX. (Contributed by Mark Alexander.)
|
||||||
characters using the \samp{u} format character. Arrays also
|
characters using the \samp{u} format character. Arrays also
|
||||||
now support using the \code{+=} assignment operator to add another array's
|
now support using the \code{+=} assignment operator to add another array's
|
||||||
contents, and the \code{*=} assignment operator to repeat an array.
|
contents, and the \code{*=} assignment operator to repeat an array.
|
||||||
(Contributed by XXX.)
|
(Contributed by Jason Orendorff.)
|
||||||
|
|
||||||
\item The \module{grp} module now returns enhanced tuples:
|
\item The \module{grp} module now returns enhanced tuples:
|
||||||
|
|
||||||
|
|
@ -518,8 +532,8 @@ Changes to Python's build process, and to the C API, include:
|
||||||
|
|
||||||
\item Python can now optionally be built as a shared library
|
\item Python can now optionally be built as a shared library
|
||||||
(\file{libpython2.3.so}) by supplying \longprogramopt{enable-shared}
|
(\file{libpython2.3.so}) by supplying \longprogramopt{enable-shared}
|
||||||
when running Python's \file{configure} script. (Contributed by XXX
|
when running Python's \file{configure} script. (Contributed by Ondrej
|
||||||
Patch \#527027)
|
Palkovsky.)
|
||||||
|
|
||||||
\item The \cfunction{PyArg_NoArgs()} macro is now deprecated, and code
|
\item The \cfunction{PyArg_NoArgs()} macro is now deprecated, and code
|
||||||
that
|
that
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue