-\chapter{Introduction}
+\chapter{Introduction\label{introduction}}
This reference manual describes the Python programming language.
It is not intended as a tutorial.
Every Python implementation comes with a number of built-in and
standard modules. These are not documented here, but in the separate
-{\em Python Library Reference} document. A few built-in modules are
+\emph{Python Library Reference} document. A few built-in modules are
mentioned when they interact in a significant way with the language
definition.
-\section{Notation}
+\section{Notation\label{notation}}
The descriptions of lexical analysis and syntax use a modified BNF
grammar notation. This uses the following style of definition:
applies both to the source character set and the run-time character
set.
-\section{Line structure}
+\section{Line structure\label{line-structure}}
A Python program is divided into a number of \emph{logical lines}.
\index{line structure}
-\subsection{Logical lines}
+\subsection{Logical lines\label{logical}}
The end of
a logical line is represented by the token NEWLINE. Statements cannot
\index{line joining}
\index{NEWLINE token}
-\subsection{Physical lines}
+\subsection{Physical lines\label{physical}}
A physical line ends in whatever the current platform's convention is
for terminating lines. On \UNIX{}, this is the \ASCII{} LF (linefeed)
followed by linefeed). On Macintosh, it is the \ASCII{} CR (return)
character.
-\subsection{Comments}
+\subsection{Comments\label{comments}}
A comment starts with a hash character (\code{\#}) that is not part of
a string literal, and ends at the end of the physical line. A comment
\index{comment}
\index{hash character}
-\subsection{Explicit line joining}
+\subsection{Explicit line joining\label{explicit-joining}}
Two or more physical lines may be joined into logical lines using
backslash characters (\code{\e}), as follows: when a physical line ends
split across physical lines using a backslash). A backslash is
illegal elsewhere on a line outside a string literal.
-\subsection{Implicit line joining}
+\subsection{Implicit line joining\label{implicit-joining}}
Expressions in parentheses, square brackets or curly braces can be
split over more than one physical line without using backslashes.
lines. Implicitly continued lines can also occur within triple-quoted
strings (see below); in that case they cannot carry comments.
-\subsection{Blank lines}
+\subsection{Blank lines\label{blank-lines}}
A logical line that contains only spaces, tabs, formfeeds and possibly a
comment, is ignored (i.e., no NEWLINE token is generated), except that
terminates a multi-line statement.
\index{blank line}
-\subsection{Indentation}
+\subsection{Indentation\label{indentation}}
Leading whitespace (spaces and tabs) at the beginning of a logical
line is used to compute the indentation level of the line, which in
last error is found by the lexical analyzer --- the indentation of
\code{return r} does not match a level popped off the stack.)
-\subsection{Whitespace between tokens}
+\subsection{Whitespace between tokens\label{whitespace}}
Except at the beginning of a logical line or in string literals, the
whitespace characters space, tab and formfeed can be used
tokens only if their concatenation could otherwise be interpreted as a
different token (e.g., ab is one token, but a b is two tokens).
-\section{Other tokens}
+\section{Other tokens\label{other-tokens}}
Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
exist: \emph{identifiers}, \emph{keywords}, \emph{literals},
ambiguity exists, a token comprises the longest possible string that
forms a legal token, when read from left to right.
-\section{Identifiers and keywords}
+\section{Identifiers and keywords\label{identifiers}}
Identifiers (also referred to as \emph{names}) are described by the following
lexical definitions:
Identifiers are unlimited in length. Case is significant.
-\subsection{Keywords}
+\subsection{Keywords\label{keywords}}
The following identifiers are used as reserved words, or
\emph{keywords} of the language, and cannot be used as ordinary
% When adding keywords, use reswords.py for reformatting
-\subsection{Reserved classes of identifiers}
+\subsection{Reserved classes of identifiers\label{id-classes}}
Certain classes of identifiers (besides keywords) have special
meanings. These are:
(XXX need section references here.)
-\section{Literals} \label{literals}
+\section{Literals\label{literals}}
Literals are notations for constant values of some built-in types.
\index{literal}
\index{constant}
-\subsection{String literals}
+\subsection{String literals\label{strings}}
String literals are described by the following lexical definitions:
\index{string literal}
backslash} (since the backslash would escape the following quote
character).
-\subsection{String literal concatenation}
+\subsection{String literal concatenation\label{string-catenation}}
Multiple adjacent string literals (delimited by whitespace), possibly
using different quoting conventions, are allowed, and their meaning is
concatenation can use different quoting styles for each component
(even mixing raw strings and triple quoted strings).
-\subsection{Numeric literals}
+\subsection{Numeric literals\label{numbers}}
There are four types of numeric literals: plain integers, long
integers, floating point numbers, and imaginary numbers. There are no
\code{-1} is actually an expression composed of the unary operator
`\code{-}' and the literal \code{1}.
-\subsection{Integer and long integer literals}
+\subsection{Integer and long integer literals\label{integers}}
Integer and long integer literals are described by the following
lexical definitions:
3L 79228162514264337593543950336L 0377L 0x100000000L
\end{verbatim}
-\subsection{Floating point literals}
+\subsection{Floating point literals\label{floating}}
Floating point literals are described by the following lexical
definitions:
\code{-1} is actually an expression composed of the operator
\code{-} and the literal \code{1}.
-\subsection{Imaginary literals}
+\subsection{Imaginary literals\label{imaginary}}
Imaginary literals are described by the following lexical definitions:
\end{verbatim}
-\section{Operators}
+\section{Operators\label{operators}}
The following tokens are operators:
\index{operators}
spellings of the same operator. \code{!=} is the preferred spelling;
\code{<>} is obsolescent.
-\section{Delimiters}
+\section{Delimiters\label{delimiters}}
The following tokens serve as delimiters in the grammar:
\index{delimiters}
-\chapter{Data model}
+\chapter{Data model\label{datamodel}}
-\section{Objects, values and types}
+\section{Objects, values and types\label{objects}}
\dfn{Objects} are Python's abstraction for data. All data in a Python
program is represented by objects or by relations between objects.
(Note that ``\code{c = d = []}'' assigns the same object to both
\code{c} and \code{d}.)
-\section{The standard type hierarchy} \label{types}
+\section{The standard type hierarchy\label{types}}
Below is a list of the types that are built into Python. Extension
modules written in \C{} can define additional types. Future versions of
\end{description} % Types
-\section{Special method names} \label{specialnames}
+\section{Special method names\label{specialnames}}
A class can implement certain operations that are invoked by special
syntax (such as arithmetic operations or subscripting and slicing) by defining
\ttindex{__getitem__}
-\subsection{Basic customization}
+\subsection{Basic customization\label{customization}}
\begin{description}
\end{description}
-\subsection{Customizing attribute access}
+\subsection{Customizing attribute access\label{attribute-access}}
The following methods can be defined to customize the meaning of
attribute access (use of, assignment to, or deletion of \code{x.name})
\end{description}
-\subsection{Emulating callable objects}
+\subsection{Emulating callable objects\label{callable-types}}
\begin{description}
\end{description}
-\subsection{Emulating sequence and mapping types}
+\subsection{Emulating sequence and mapping types\label{sequence-types}}
The following methods can be defined to emulate sequence or mapping
objects. The first set of methods is used either to emulate a
\end{description}
-\subsection{Additional methods for emulation of sequence types}
+\subsection{Additional methods for emulation of sequence types%
+ \label{sequence-methods}}
The following methods can be defined to further emulate sequence
objects. Immutable sequences methods should only define
notation, \method{__getitem__()}, \method{__setitem__()}
or\method{__delitem__()} is called.
-\subsection{Emulating numeric types}
+\subsection{Emulating numeric types\label{numeric-types}}
The following methods can be defined to emulate numeric objects.
Methods corresponding to operations that are not supported by the
\chapter{Execution model\label{execmodel}}
\index{execution model}
-\section{Code blocks, execution frames, and namespaces} \label{execframes}
+\section{Code blocks, execution frames, and namespaces\label{execframes}}
\index{code block}
\indexii{execution}{frame}
\index{namespace}
the optimizer may cause the local namespace to be implemented
differently, and \function{locals()} returns a read-only dictionary.}
-\section{Exceptions}
+\section{Exceptions\label{exceptions}}
Exceptions are a means of breaking out of the normal flow of control
of a code block in order to handle errors or other exceptional
-\chapter{Simple statements}
+\chapter{Simple statements\label{simple}}
\indexii{simple}{statement}
Simple statements are comprised within a single logical line.
| exec_stmt
\end{verbatim}
-\section{Expression statements}
+\section{Expression statements\label{exprstmts}}
\indexii{expression}{statement}
Expression statements are used (mostly interactively) to compute and
\indexii{writing}{values}
\indexii{procedure}{call}
-\section{Assert statements}\stindex{assert}
+\section{Assert statements\label{assert}}\stindex{assert}
Assert statements are a convenient way to insert debugging
assertions\indexii{debugging}{assertions} into a program:
it will be displayed as part of the stack trace.
-\section{Assignment statements}
+\section{Assignment statements\label{assignment}}
\indexii{assignment}{statement}
Assignment statements are used to (re)bind names to values and to
\end{verbatim}
-\section{The \keyword{pass} statement}
+\section{The \keyword{pass} statement\label{pass}}
\stindex{pass}
\begin{verbatim}
class C: pass # a class with no methods (yet)
\end{verbatim}
-\section{The \keyword{del} statement}
+\section{The \keyword{del} statement\label{del}}
\stindex{del}
\begin{verbatim}
right type (but even this is determined by the sliced object).
\indexii{attribute}{deletion}
-\section{The \keyword{print} statement} \label{print}
+\section{The \keyword{print} statement\label{print}}
\stindex{print}
\begin{verbatim}
\ttindex{stdout}
\exindex{RuntimeError}
-\section{The \keyword{return} statement}
+\section{The \keyword{return} statement\label{return}}
\stindex{return}
\begin{verbatim}
before really leaving the function.
\kwindex{finally}
-\section{The \keyword{raise} statement}
+\section{The \keyword{raise} statement\label{raise}}
\stindex{raise}
\begin{verbatim}
transparently in an except clause.
\obindex{traceback}
-\section{The \keyword{break} statement}
+\section{The \keyword{break} statement\label{break}}
\stindex{break}
\begin{verbatim}
before really leaving the loop.
\kwindex{finally}
-\section{The \keyword{continue} statement}
+\section{The \keyword{continue} statement\label{continue}}
\stindex{continue}
\begin{verbatim}
\indexii{loop}{statement}
\kwindex{finally}
-\section{The \keyword{import} statement} \label{import}
+\section{The \keyword{import} statement\label{import}}
\stindex{import}
\begin{verbatim}
[XXX Also should mention __import__().]
\bifuncindex{__import__}
-\section{The \keyword{global} statement} \label{global}
+\section{The \keyword{global} statement\label{global}}
\stindex{global}
\begin{verbatim}
\bifuncindex{execfile}
\bifuncindex{compile}
-\section{The {\tt exec} statement} \label{exec}
+\section{The \keyword{exec} statement\label{exec}}
\stindex{exec}
\begin{verbatim}
-\chapter{Compound statements}
+\chapter{Compound statements\label{compound}}
\indexii{compound}{statement}
Compound statements contain (groups of) other statements; they affect
The formatting of the grammar rules in the following sections places
each clause on a separate line for clarity.
-\section{The \keyword{if} statement}
+\section{The \keyword{if} statement\label{if}}
\stindex{if}
The \keyword{if} statement is used for conditional execution:
\kwindex{elif}
\kwindex{else}
-\section{The \keyword{while} statement}
+\section{The \keyword{while} statement\label{while}}
\stindex{while}
\indexii{loop}{statement}
\stindex{break}
\stindex{continue}
-\section{The \keyword{for} statement}
+\section{The \keyword{for} statement\label{for}}
\stindex{for}
\indexii{loop}{statement}
if x < 0: a.remove(x)
\end{verbatim}
-\section{The \keyword{try} statement} \label{try}
+\section{The \keyword{try} statement\label{try}}
\stindex{try}
The \keyword{try} statement specifies exception handlers and/or cleanup
\stindex{break}
\stindex{continue}
-\section{Function definitions} \label{function}
+\section{Function definitions\label{function}}
\indexii{function}{definition}
A function definition defines a user-defined function object (see
print add1(3) # This prints '4'
\end{verbatim}
-\section{Class definitions} \label{class}
+\section{Class definitions\label{class}}
\indexii{class}{definition}
A class definition defines a class object (see section \ref{types}):
-\chapter{Top-level components}
+\chapter{Top-level components\label{top-level}}
The Python interpreter can get its input from a number of sources:
from a script passed to it as standard input or as program argument,
gives the syntax used in these cases.
\index{interpreter}
-\section{Complete Python programs}
+\section{Complete Python programs\label{programs}}
\index{program}
While a language specification need not prescribe how the language
\index{command line}
\index{standard input}
-\section{File input}
+\section{File input\label{file-input}}
All input read from non-interactive files has the same form:
\end{itemize}
-\section{Interactive input}
+\section{Interactive input\label{interactive}}
Input in interactive mode is parsed using the following grammar:
line in interactive mode; this is needed to help the parser detect the
end of the input.
-\section{Expression input}
+\section{Expression input\label{expression-input}}
\index{input}
There are two forms of expression input. Both ignore leading