-\chapter{Lexical analysis}
+\chapter{Lexical analysis\label{lexical}}
A Python program is read by a \emph{parser}. Input to the parser is a
stream of \emph{tokens}, generated by the \emph{lexical analyzer}. This
A Python program is divided into a number of \emph{logical lines}.
\index{line structure}
-\subsection{Logical Lines}
+\subsection{Logical lines}
The end of
a logical line is represented by the token NEWLINE. Statements cannot
Certain classes of identifiers (besides keywords) have special
meanings. These are:
-\begin{center}
-\begin{tabular}{|l|l|}
-\hline
-Form & Meaning \\
-\hline
-\code{_*} & Not imported by \code{from \var{module} import *} \\
-\code{__*__} & System-defined name \\
-\code{__*} & Class-private name mangling \\
-\hline
-\end{tabular}
-\end{center}
+\begin{tableii}{l|l}{code}{Form}{Meaning}
+\lineii{_*}{Not imported by \samp{from \var{module} import *}}
+\lineii{__*__}{System-defined name}
+\lineii{__*}{Class-private name mangling}
+\end{tableii}
(XXX need section references here.)
\index{Standard C}
\index{C}
-\begin{center}
-\begin{tabular}{|l|l|}
-\hline
-Escape Sequence & Meaning \\
-\hline
-\code{\e}\emph{newline} & Ignored \\
-\code{\e\e} & Backslash (\code{\e}) \\
-\code{\e'} & Single quote (\code{'}) \\
-\code{\e"} & Double quote (\code{"}) \\
-\code{\e a} & \ASCII{} Bell (BEL) \\
-\code{\e b} & \ASCII{} Backspace (BS) \\
-\code{\e f} & \ASCII{} Formfeed (FF) \\
-\code{\e n} & \ASCII{} Linefeed (LF) \\
-\code{\e r} & \ASCII{} Carriage Return (CR) \\
-\code{\e t} & \ASCII{} Horizontal Tab (TAB) \\
-\code{\e v} & \ASCII{} Vertical Tab (VT) \\
-\code{\e}\emph{ooo} & \ASCII{} character with octal value \emph{ooo} \\
-\code{\e x}\emph{hh...} & \ASCII{} character with hex value \emph{hh...} \\
-\hline
-\end{tabular}
-\end{center}
+\begin{tableii}{l|l}{code}{Escape Sequence}{Meaning}
+\lineii{\e\var{newline}} {Ignored}
+\lineii{\e\e} {Backslash (\code{\e})}
+\lineii{\e'} {Single quote (\code{'})}
+\lineii{\e"} {Double quote (\code{"})}
+\lineii{\e a} {\ASCII{} Bell (BEL)}
+\lineii{\e b} {\ASCII{} Backspace (BS)}
+\lineii{\e f} {\ASCII{} Formfeed (FF)}
+\lineii{\e n} {\ASCII{} Linefeed (LF)}
+\lineii{\e r} {\ASCII{} Carriage Return (CR)}
+\lineii{\e t} {\ASCII{} Horizontal Tab (TAB)}
+\lineii{\e v} {\ASCII{} Vertical Tab (VT)}
+\lineii{\e\var{ooo}} {\ASCII{} character with octal value \emph{ooo}}
+\lineii{\e x\var{hh...}} {\ASCII{} character with hex value \emph{hh...}}
+\end{tableii}
\index{ASCII@\ASCII{}}
In strict compatibility with Standard \C, up to three octal digits are
-\chapter{Execution model}
+\chapter{Execution model\label{execmodel}}
\index{execution model}
\section{Code blocks, execution frames, and namespaces} \label{execframes}
\indexii{execution}{frame}
\index{namespace}
-A {\em code block} is a piece of Python program text that can be
+A \dfn{code block} is a piece of Python program text that can be
executed as a unit, such as a module, a class definition or a function
body. Some code blocks (like modules) are normally executed only once, others
(like function bodies) may be executed many times. Code blocks may
read and evaluated by the built-in function \function{input()} is a
code block.
-A code block is executed in an execution frame. An {\em execution
+A code block is executed in an execution frame. An \dfn{execution
frame} contains some administrative information (used for debugging),
determines where and how execution continues after the code block's
execution has completed, and (perhaps most importantly) defines two
execution of the code block.
\indexii{execution}{frame}
-A {\em namespace} is a mapping from names (identifiers) to objects.
+A \dfn{namespace} is a mapping from names (identifiers) to objects.
A particular namespace may be referenced by more than one execution
frame, and from other places as well. Adding a name to a namespace
-is called {\em binding} a name (to an object); changing the mapping of
-a name is called {\em rebinding}; removing a name is {\em unbinding}.
+is called \dfn{binding} a name (to an object); changing the mapping of
+a name is called \dfn{rebinding}; removing a name is \dfn{unbinding}.
Namespaces are functionally equivalent to dictionaries (and often
implemented as dictionaries).
\index{namespace}
\indexii{rebinding}{name}
\indexii{unbinding}{name}
-The {\em local namespace} of an execution frame determines the default
-place where names are defined and searched. The {\em global
+The \dfn{local namespace} of an execution frame determines the default
+place where names are defined and searched. The \dfn{global
namespace} determines the place where names listed in \keyword{global}
statements are defined and searched, and where names that are not
bound anywhere in the current code block are searched.
the global namespace is the namespace of the containing module ---
scopes in Python do not nest!
-\begin{center}
-\begin{tabular}{|l|l|l|l|}
-\hline
-Code block type & Global namespace & Local namespace & Notes \\
-\hline
-Module & n.s. for this module & same as global & \\
-Script (file or command) & n.s. for \module{__main__} & same as global
- & (1) \\
-Interactive command & n.s. for \module{__main__} & same as global & \\
-Class definition & global n.s. of containing block & new n.s. & \\
-Function body & global n.s. of containing block & new n.s. & (2) \\
-String passed to \keyword{exec} statement
- & global n.s. of containing block
- & local n.s. of containing block & (2), (3) \\
-String passed to \function{eval()}
- & global n.s. of caller & local n.s. of caller & (2), (3) \\
-File read by \function{execfile()}
- & global n.s. of caller & local n.s. of caller & (2), (3) \\
-Expression read by \function{input()}
- & global n.s. of caller & local n.s. of caller & \\
-\hline
-\end{tabular}
-\end{center}
+\begin{tableiv}{l|l|l|l}{textrm}%
+ {Code block type}{Global namespace}{Local namespace}{Notes}
+ \lineiv{Module}%
+ {n.s. for this module}%
+ {same as global}{}
+ \lineiv{Script (file or command)}%
+ {n.s. for \module{__main__}}%
+ {same as global}{(1)}
+ \lineiv{Interactive command}%
+ {n.s. for \module{__main__}}%
+ {same as global}{}
+ \lineiv{Class definition}%
+ {global n.s. of containing block}%
+ {new n.s.}{}
+ \lineiv{Function body}%
+ {global n.s. of containing block}%
+ {new n.s.}{(2)}
+ \lineiv{String passed to \keyword{exec} statement}%
+ {global n.s. of containing block}%
+ {local n.s. of containing block}{(2), (3)}
+ \lineiv{String passed to \function{eval()}}%
+ {global n.s. of caller}%
+ {local n.s. of caller}{(2), (3)}
+ \lineiv{File read by \function{execfile()}}%
+ {global n.s. of caller}%
+ {local n.s. of caller}{(2), (3)}
+ \lineiv{Expression read by \function{input()}}%
+ {global n.s. of caller}%
+ {local n.s. of caller}{}
+\end{tableiv}
\refbimodindex{__main__}
Notes:
\begin{description}
-\item[n.s.] means {\em namespace}
+\item[n.s.] means \emph{namespace}
\item[(1)] The main module for a script is always called
\module{__main__}; ``the filename don't enter into it.''
respectively. The effect of modifications to this dictionary on the
namespace are undefined.%
\footnote{The current implementations return the dictionary actually
-used to implement the namespace, {\em except} for functions, where
+used to implement the namespace, \emph{except} for functions, where
the optimizer may cause the local namespace to be implemented
differently, and \function{locals()} returns a read-only dictionary.}
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
-conditions. An exception is {\em raised} at the point where the error
-is detected; it may be {\em handled} by the surrounding code block or
+conditions. An exception is \emph{raised} at the point where the error
+is detected; it may be \emph{handled} by the surrounding code block or
by any code block that directly or indirectly invoked the code block
where the error occurred.
\index{exception}