mirror of
https://github.com/AdaCore/cpython.git
synced 2026-02-12 12:57:15 -08:00
Removed LaTeX version of reference manual. Added ref/ref.ps.
This commit is contained in:
31
Doc/Makefile
31
Doc/Makefile
@@ -6,9 +6,14 @@
|
||||
# This is a bit of a mess. The main documents are:
|
||||
# tut -- Tutorial (file tut.tex)
|
||||
# lib -- Library Reference (file lib.tex, inputs lib*.tex)
|
||||
# ref -- Language Reference (file ref.tex, inputs ref*.tex)
|
||||
# ext -- Extending and Embedding (file ext.tex)
|
||||
#
|
||||
# The Reference Manual is now maintained as a FrameMaker document.
|
||||
# See the subdirectory ref; PostScript is included as ref/ref.ps.
|
||||
# (In the future, the Tutorial will also be converted to FrameMaker;
|
||||
# the other documents will be maintained in a text format such
|
||||
# as LaTeX or perhaps TIM.)
|
||||
#
|
||||
# The main target "make all" creates DVI and PostScript for these
|
||||
# four. You can also do "make lib" (etc.) to process individual
|
||||
# documents.
|
||||
@@ -74,20 +79,19 @@ DOCDESTDIR= $LIBDEST/doc
|
||||
# Main target
|
||||
all: all-ps
|
||||
|
||||
all-dvi: tut.dvi lib.dvi ref.dvi ext.dvi
|
||||
all-ps: tut.ps lib.ps ref.ps ext.ps
|
||||
all-dvi: tut.dvi lib.dvi ext.dvi
|
||||
all-ps: tut.ps lib.ps ext.ps
|
||||
|
||||
# Individual document fake targets
|
||||
tut: tut.ps
|
||||
lib: lib.ps
|
||||
ref: ref.ps
|
||||
ext: ext.ps
|
||||
|
||||
# CWI Quarterly document fake target
|
||||
qua: qua.ps
|
||||
|
||||
# Dependencies
|
||||
tut.dvi lib.dvi ref.dvi ext.dvi: myformat.sty fix_hack
|
||||
tut.dvi lib.dvi ext.dvi: myformat.sty fix_hack
|
||||
|
||||
# Tutorial document
|
||||
tut.dvi: tut.tex
|
||||
@@ -97,18 +101,6 @@ tut.dvi: tut.tex
|
||||
tut.ps: tut.dvi
|
||||
$(DVIPS) tut >tut.ps
|
||||
|
||||
# Reference document
|
||||
ref.dvi: ref.tex ref1.tex ref2.tex ref3.tex ref4.tex ref5.tex ref6.tex \
|
||||
ref7.tex ref8.tex
|
||||
touch ref.ind
|
||||
$(LATEX) ref
|
||||
./fix_hack ref.idx
|
||||
$(MAKEINDEX) ref
|
||||
$(LATEX) ref
|
||||
|
||||
ref.ps: ref.dvi
|
||||
$(DVIPS) ref >ref.ps
|
||||
|
||||
# LaTeX source files for the Python Library Reference
|
||||
LIBFILES = lib.tex \
|
||||
libintro.tex libobjs.tex libtypes.tex libexcs.tex libfuncs.tex \
|
||||
@@ -216,11 +208,6 @@ l2htut: tut.dvi
|
||||
@rm -rf python-tut
|
||||
mv tut python-tut
|
||||
|
||||
l2href: ref.dvi
|
||||
$(L2H) $(L2HARGS) ref.tex
|
||||
@rm -rf python-ref
|
||||
mv ref python-ref
|
||||
|
||||
l2hext: ext.dvi
|
||||
$(L2H) $(L2HARGS) ext.tex
|
||||
@rm -rf python-ext
|
||||
|
||||
68
Doc/ref.tex
68
Doc/ref.tex
@@ -1,68 +0,0 @@
|
||||
\documentstyle[twoside,11pt,myformat]{report}
|
||||
|
||||
\title{Python Reference Manual}
|
||||
|
||||
\input{boilerplate}
|
||||
|
||||
% Tell \index to actually write the .idx file
|
||||
\makeindex
|
||||
|
||||
\begin{document}
|
||||
|
||||
\pagenumbering{roman}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{copyright}
|
||||
|
||||
\begin{abstract}
|
||||
|
||||
\noindent
|
||||
Python is a simple, yet powerful, interpreted programming language
|
||||
that bridges the gap between C and shell programming, and is thus
|
||||
ideally suited for ``throw-away programming'' and rapid prototyping.
|
||||
Its syntax is put together from constructs borrowed from a variety of
|
||||
other languages; most prominent are influences from ABC, C, Modula-3
|
||||
and Icon.
|
||||
|
||||
The Python interpreter is easily extended with new functions and data
|
||||
types implemented in C. Python is also suitable as an extension
|
||||
language for highly customizable C applications such as editors or
|
||||
window managers.
|
||||
|
||||
Python is available for various operating systems, amongst which
|
||||
several flavors of {\UNIX} (including Linux), the Apple Macintosh O.S.,
|
||||
MS-DOS, MS-Windows 3.1, Windows NT, and OS/2.
|
||||
|
||||
This reference manual describes the syntax and ``core semantics'' of
|
||||
the language. It is terse, but attempts to be exact and complete.
|
||||
The semantics of non-essential built-in object types and of the
|
||||
built-in functions and modules are described in the {\em Python
|
||||
Library Reference}. For an informal introduction to the language, see
|
||||
the {\em Python Tutorial}.
|
||||
|
||||
\end{abstract}
|
||||
|
||||
\pagebreak
|
||||
|
||||
{
|
||||
\parskip = 0mm
|
||||
\tableofcontents
|
||||
}
|
||||
|
||||
\pagebreak
|
||||
|
||||
\pagenumbering{arabic}
|
||||
|
||||
\include{ref1} % Introduction
|
||||
\include{ref2} % Lexical analysis
|
||||
\include{ref3} % Data model
|
||||
\include{ref4} % Execution model
|
||||
\include{ref5} % Expressions and conditions
|
||||
\include{ref6} % Simple statements
|
||||
\include{ref7} % Compound statements
|
||||
\include{ref8} % Top-level components
|
||||
|
||||
\input{ref.ind}
|
||||
|
||||
\end{document}
|
||||
16373
Doc/ref/ref.ps
Normal file
16373
Doc/ref/ref.ps
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,68 +0,0 @@
|
||||
\documentstyle[twoside,11pt,myformat]{report}
|
||||
|
||||
\title{Python Reference Manual}
|
||||
|
||||
\input{boilerplate}
|
||||
|
||||
% Tell \index to actually write the .idx file
|
||||
\makeindex
|
||||
|
||||
\begin{document}
|
||||
|
||||
\pagenumbering{roman}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{copyright}
|
||||
|
||||
\begin{abstract}
|
||||
|
||||
\noindent
|
||||
Python is a simple, yet powerful, interpreted programming language
|
||||
that bridges the gap between C and shell programming, and is thus
|
||||
ideally suited for ``throw-away programming'' and rapid prototyping.
|
||||
Its syntax is put together from constructs borrowed from a variety of
|
||||
other languages; most prominent are influences from ABC, C, Modula-3
|
||||
and Icon.
|
||||
|
||||
The Python interpreter is easily extended with new functions and data
|
||||
types implemented in C. Python is also suitable as an extension
|
||||
language for highly customizable C applications such as editors or
|
||||
window managers.
|
||||
|
||||
Python is available for various operating systems, amongst which
|
||||
several flavors of {\UNIX} (including Linux), the Apple Macintosh O.S.,
|
||||
MS-DOS, MS-Windows 3.1, Windows NT, and OS/2.
|
||||
|
||||
This reference manual describes the syntax and ``core semantics'' of
|
||||
the language. It is terse, but attempts to be exact and complete.
|
||||
The semantics of non-essential built-in object types and of the
|
||||
built-in functions and modules are described in the {\em Python
|
||||
Library Reference}. For an informal introduction to the language, see
|
||||
the {\em Python Tutorial}.
|
||||
|
||||
\end{abstract}
|
||||
|
||||
\pagebreak
|
||||
|
||||
{
|
||||
\parskip = 0mm
|
||||
\tableofcontents
|
||||
}
|
||||
|
||||
\pagebreak
|
||||
|
||||
\pagenumbering{arabic}
|
||||
|
||||
\include{ref1} % Introduction
|
||||
\include{ref2} % Lexical analysis
|
||||
\include{ref3} % Data model
|
||||
\include{ref4} % Execution model
|
||||
\include{ref5} % Expressions and conditions
|
||||
\include{ref6} % Simple statements
|
||||
\include{ref7} % Compound statements
|
||||
\include{ref8} % Top-level components
|
||||
|
||||
\input{ref.ind}
|
||||
|
||||
\end{document}
|
||||
@@ -1,81 +0,0 @@
|
||||
\chapter{Introduction}
|
||||
|
||||
This reference manual describes the Python programming language.
|
||||
It is not intended as a tutorial.
|
||||
|
||||
While I am trying to be as precise as possible, I chose to use English
|
||||
rather than formal specifications for everything except syntax and
|
||||
lexical analysis. This should make the document more understandable
|
||||
to the average reader, but will leave room for ambiguities.
|
||||
Consequently, if you were coming from Mars and tried to re-implement
|
||||
Python from this document alone, you might have to guess things and in
|
||||
fact you would probably end up implementing quite a different language.
|
||||
On the other hand, if you are using
|
||||
Python and wonder what the precise rules about a particular area of
|
||||
the language are, you should definitely be able to find them here.
|
||||
|
||||
It is dangerous to add too many implementation details to a language
|
||||
reference document --- the implementation may change, and other
|
||||
implementations of the same language may work differently. On the
|
||||
other hand, there is currently only one Python implementation, and
|
||||
its particular quirks are sometimes worth being mentioned, especially
|
||||
where the implementation imposes additional limitations. Therefore,
|
||||
you'll find short ``implementation notes'' sprinkled throughout the
|
||||
text.
|
||||
|
||||
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
|
||||
mentioned when they interact in a significant way with the language
|
||||
definition.
|
||||
|
||||
\section{Notation}
|
||||
|
||||
The descriptions of lexical analysis and syntax use a modified BNF
|
||||
grammar notation. This uses the following style of definition:
|
||||
\index{BNF}
|
||||
\index{grammar}
|
||||
\index{syntax}
|
||||
\index{notation}
|
||||
|
||||
\begin{verbatim}
|
||||
name: lc_letter (lc_letter | "_")*
|
||||
lc_letter: "a"..."z"
|
||||
\end{verbatim}
|
||||
|
||||
The first line says that a \verb@name@ is an \verb@lc_letter@ followed by
|
||||
a sequence of zero or more \verb@lc_letter@s and underscores. An
|
||||
\verb@lc_letter@ in turn is any of the single characters `a' through `z'.
|
||||
(This rule is actually adhered to for the names defined in lexical and
|
||||
grammar rules in this document.)
|
||||
|
||||
Each rule begins with a name (which is the name defined by the rule)
|
||||
and a colon. A vertical bar (\verb@|@) is used to separate
|
||||
alternatives; it is the least binding operator in this notation. A
|
||||
star (\verb@*@) means zero or more repetitions of the preceding item;
|
||||
likewise, a plus (\verb@+@) means one or more repetitions, and a
|
||||
phrase enclosed in square brackets (\verb@[ ]@) means zero or one
|
||||
occurrences (in other words, the enclosed phrase is optional). The
|
||||
\verb@*@ and \verb@+@ operators bind as tightly as possible;
|
||||
parentheses are used for grouping. Literal strings are enclosed in
|
||||
quotes. White space is only meaningful to separate tokens.
|
||||
Rules are normally contained on a single line; rules with many
|
||||
alternatives may be formatted alternatively with each line after the
|
||||
first beginning with a vertical bar.
|
||||
|
||||
In lexical definitions (as the example above), two more conventions
|
||||
are used: Two literal characters separated by three dots mean a choice
|
||||
of any single character in the given (inclusive) range of \ASCII{}
|
||||
characters. A phrase between angular brackets (\verb@<...>@) gives an
|
||||
informal description of the symbol defined; e.g. this could be used
|
||||
to describe the notion of `control character' if needed.
|
||||
\index{lexical definitions}
|
||||
\index{ASCII}
|
||||
|
||||
Even though the notation used is almost the same, there is a big
|
||||
difference between the meaning of lexical and syntactic definitions:
|
||||
a lexical definition operates on the individual characters of the
|
||||
input source, while a syntax definition operates on the stream of
|
||||
tokens generated by the lexical analysis. All uses of BNF in the next
|
||||
chapter (``Lexical Analysis'') are lexical definitions; uses in
|
||||
subsequent chapters are syntactic definitions.
|
||||
372
Doc/ref/ref2.tex
372
Doc/ref/ref2.tex
@@ -1,372 +0,0 @@
|
||||
\chapter{Lexical analysis}
|
||||
|
||||
A Python program is read by a {\em parser}. Input to the parser is a
|
||||
stream of {\em tokens}, generated by the {\em lexical analyzer}. This
|
||||
chapter describes how the lexical analyzer breaks a file into tokens.
|
||||
\index{lexical analysis}
|
||||
\index{parser}
|
||||
\index{token}
|
||||
|
||||
\section{Line structure}
|
||||
|
||||
A Python program is divided in a number of logical lines. The end of
|
||||
a logical line is represented by the token NEWLINE. Statements cannot
|
||||
cross logical line boundaries except where NEWLINE is allowed by the
|
||||
syntax (e.g. between statements in compound statements).
|
||||
\index{line structure}
|
||||
\index{logical line}
|
||||
\index{NEWLINE token}
|
||||
|
||||
\subsection{Comments}
|
||||
|
||||
A comment starts with a hash character (\verb@#@) that is not part of
|
||||
a string literal, and ends at the end of the physical line. A comment
|
||||
always signifies the end of the logical line. Comments are ignored by
|
||||
the syntax.
|
||||
\index{comment}
|
||||
\index{logical line}
|
||||
\index{physical line}
|
||||
\index{hash character}
|
||||
|
||||
\subsection{Explicit line joining}
|
||||
|
||||
Two or more physical lines may be joined into logical lines using
|
||||
backslash characters (\verb/\/), as follows: when a physical line ends
|
||||
in a backslash that is not part of a string literal or comment, it is
|
||||
joined with the following forming a single logical line, deleting the
|
||||
backslash and the following end-of-line character. For example:
|
||||
\index{physical line}
|
||||
\index{line joining}
|
||||
\index{line continuation}
|
||||
\index{backslash character}
|
||||
%
|
||||
\begin{verbatim}
|
||||
if 1900 < year < 2100 and 1 <= month <= 12 \
|
||||
and 1 <= day <= 31 and 0 <= hour < 24 \
|
||||
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
|
||||
return 1
|
||||
\end{verbatim}
|
||||
|
||||
A line ending in a backslash cannot carry a comment; a backslash does
|
||||
not continue a comment (but it does continue a string literal, see
|
||||
below).
|
||||
|
||||
\subsection{Implicit line joining}
|
||||
|
||||
Expressions in parentheses, square brackets or curly braces can be
|
||||
split over more than one physical line without using backslashes.
|
||||
For example:
|
||||
|
||||
\begin{verbatim}
|
||||
month_names = ['Januari', 'Februari', 'Maart', # These are the
|
||||
'April', 'Mei', 'Juni', # Dutch names
|
||||
'Juli', 'Augustus', 'September', # for the months
|
||||
'Oktober', 'November', 'December'] # of the year
|
||||
\end{verbatim}
|
||||
|
||||
Implicitly continued lines can carry comments. The indentation of the
|
||||
continuation lines is not important. Blank continuation lines are
|
||||
allowed.
|
||||
|
||||
\subsection{Blank lines}
|
||||
|
||||
A logical line that contains only spaces, tabs, and possibly a
|
||||
comment, is ignored (i.e., no NEWLINE token is generated), except that
|
||||
during interactive input of statements, an entirely blank logical line
|
||||
terminates a multi-line statement.
|
||||
\index{blank line}
|
||||
|
||||
\subsection{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
|
||||
turn is used to determine the grouping of statements.
|
||||
\index{indentation}
|
||||
\index{whitespace}
|
||||
\index{leading whitespace}
|
||||
\index{space}
|
||||
\index{tab}
|
||||
\index{grouping}
|
||||
\index{statement grouping}
|
||||
|
||||
First, tabs are replaced (from left to right) by one to eight spaces
|
||||
such that the total number of characters up to there is a multiple of
|
||||
eight (this is intended to be the same rule as used by {\UNIX}). The
|
||||
total number of spaces preceding the first non-blank character then
|
||||
determines the line's indentation. Indentation cannot be split over
|
||||
multiple physical lines using backslashes.
|
||||
|
||||
The indentation levels of consecutive lines are used to generate
|
||||
INDENT and DEDENT tokens, using a stack, as follows.
|
||||
\index{INDENT token}
|
||||
\index{DEDENT token}
|
||||
|
||||
Before the first line of the file is read, a single zero is pushed on
|
||||
the stack; this will never be popped off again. The numbers pushed on
|
||||
the stack will always be strictly increasing from bottom to top. At
|
||||
the beginning of each logical line, the line's indentation level is
|
||||
compared to the top of the stack. If it is equal, nothing happens.
|
||||
If it is larger, it is pushed on the stack, and one INDENT token is
|
||||
generated. If it is smaller, it {\em must} be one of the numbers
|
||||
occurring on the stack; all numbers on the stack that are larger are
|
||||
popped off, and for each number popped off a DEDENT token is
|
||||
generated. At the end of the file, a DEDENT token is generated for
|
||||
each number remaining on the stack that is larger than zero.
|
||||
|
||||
Here is an example of a correctly (though confusingly) indented piece
|
||||
of Python code:
|
||||
|
||||
\begin{verbatim}
|
||||
def perm(l):
|
||||
# Compute the list of all permutations of l
|
||||
|
||||
if len(l) <= 1:
|
||||
return [l]
|
||||
r = []
|
||||
for i in range(len(l)):
|
||||
s = l[:i] + l[i+1:]
|
||||
p = perm(s)
|
||||
for x in p:
|
||||
r.append(l[i:i+1] + x)
|
||||
return r
|
||||
\end{verbatim}
|
||||
|
||||
The following example shows various indentation errors:
|
||||
|
||||
\begin{verbatim}
|
||||
def perm(l): # error: first line indented
|
||||
for i in range(len(l)): # error: not indented
|
||||
s = l[:i] + l[i+1:]
|
||||
p = perm(l[:i] + l[i+1:]) # error: unexpected indent
|
||||
for x in p:
|
||||
r.append(l[i:i+1] + x)
|
||||
return r # error: inconsistent dedent
|
||||
\end{verbatim}
|
||||
|
||||
(Actually, the first three errors are detected by the parser; only the
|
||||
last error is found by the lexical analyzer --- the indentation of
|
||||
\verb@return r@ does not match a level popped off the stack.)
|
||||
|
||||
\section{Other tokens}
|
||||
|
||||
Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
|
||||
exist: identifiers, keywords, literals, operators, and delimiters.
|
||||
Spaces and tabs are not tokens, but serve to delimit tokens. Where
|
||||
ambiguity exists, a token comprises the longest possible string that
|
||||
forms a legal token, when read from left to right.
|
||||
|
||||
\section{Identifiers}
|
||||
|
||||
Identifiers (also referred to as names) are described by the following
|
||||
lexical definitions:
|
||||
\index{identifier}
|
||||
\index{name}
|
||||
|
||||
\begin{verbatim}
|
||||
identifier: (letter|"_") (letter|digit|"_")*
|
||||
letter: lowercase | uppercase
|
||||
lowercase: "a"..."z"
|
||||
uppercase: "A"..."Z"
|
||||
digit: "0"..."9"
|
||||
\end{verbatim}
|
||||
|
||||
Identifiers are unlimited in length. Case is significant.
|
||||
|
||||
\subsection{Keywords}
|
||||
|
||||
The following identifiers are used as reserved words, or {\em
|
||||
keywords} of the language, and cannot be used as ordinary
|
||||
identifiers. They must be spelled exactly as written here:
|
||||
\index{keyword}
|
||||
\index{reserved word}
|
||||
|
||||
\begin{verbatim}
|
||||
and elif global not try
|
||||
break else if or while
|
||||
class except import pass
|
||||
continue finally in print
|
||||
def for is raise
|
||||
del from lambda return
|
||||
\end{verbatim}
|
||||
|
||||
% When adding keywords, pipe it through keywords.py for reformatting
|
||||
|
||||
\section{Literals} \label{literals}
|
||||
|
||||
Literals are notations for constant values of some built-in types.
|
||||
\index{literal}
|
||||
\index{constant}
|
||||
|
||||
\subsection{String literals}
|
||||
|
||||
String literals are described by the following lexical definitions:
|
||||
\index{string literal}
|
||||
|
||||
\begin{verbatim}
|
||||
stringliteral: shortstring | longstring
|
||||
shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
|
||||
longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
|
||||
shortstringitem: shortstringchar | escapeseq
|
||||
longstringitem: longstringchar | escapeseq
|
||||
shortstringchar: <any ASCII character except "\" or newline or the quote>
|
||||
longstringchar: <any ASCII character except "\">
|
||||
escapeseq: "\" <any ASCII character>
|
||||
\end{verbatim}
|
||||
\index{ASCII}
|
||||
|
||||
In ``long strings'' (strings surrounded by sets of three quotes),
|
||||
unescaped newlines and quotes are allowed (and are retained), except
|
||||
that three unescaped quotes in a row terminate the string. (A
|
||||
``quote'' is the character used to open the string, i.e. either
|
||||
\verb/'/ or \verb/"/.)
|
||||
|
||||
Escape sequences in strings are interpreted according to rules similar
|
||||
to those used by Standard C. The recognized escape sequences are:
|
||||
\index{physical line}
|
||||
\index{escape sequence}
|
||||
\index{Standard C}
|
||||
\index{C}
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{|l|l|}
|
||||
\hline
|
||||
\verb/\/{\em newline} & Ignored \\
|
||||
\verb/\\/ & Backslash (\verb/\/) \\
|
||||
\verb/\'/ & Single quote (\verb/'/) \\
|
||||
\verb/\"/ & Double quote (\verb/"/) \\
|
||||
\verb/\a/ & \ASCII{} Bell (BEL) \\
|
||||
\verb/\b/ & \ASCII{} Backspace (BS) \\
|
||||
%\verb/\E/ & \ASCII{} Escape (ESC) \\
|
||||
\verb/\f/ & \ASCII{} Formfeed (FF) \\
|
||||
\verb/\n/ & \ASCII{} Linefeed (LF) \\
|
||||
\verb/\r/ & \ASCII{} Carriage Return (CR) \\
|
||||
\verb/\t/ & \ASCII{} Horizontal Tab (TAB) \\
|
||||
\verb/\v/ & \ASCII{} Vertical Tab (VT) \\
|
||||
\verb/\/{\em ooo} & \ASCII{} character with octal value {\em ooo} \\
|
||||
\verb/\x/{\em xx...} & \ASCII{} character with hex value {\em xx...} \\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
\index{ASCII}
|
||||
|
||||
In strict compatibility with Standard C, up to three octal digits are
|
||||
accepted, but an unlimited number of hex digits is taken to be part of
|
||||
the hex escape (and then the lower 8 bits of the resulting hex number
|
||||
are used in all current implementations...).
|
||||
|
||||
All unrecognized escape sequences are left in the string unchanged,
|
||||
i.e., {\em the backslash is left in the string.} (This behavior is
|
||||
useful when debugging: if an escape sequence is mistyped, the
|
||||
resulting output is more easily recognized as broken. It also helps a
|
||||
great deal for string literals used as regular expressions or
|
||||
otherwise passed to other modules that do their own escape handling.)
|
||||
\index{unrecognized escape sequence}
|
||||
|
||||
\subsection{Numeric literals}
|
||||
|
||||
There are three types of numeric literals: plain integers, long
|
||||
integers, and floating point numbers.
|
||||
\index{number}
|
||||
\index{numeric literal}
|
||||
\index{integer literal}
|
||||
\index{plain integer literal}
|
||||
\index{long integer literal}
|
||||
\index{floating point literal}
|
||||
\index{hexadecimal literal}
|
||||
\index{octal literal}
|
||||
\index{decimal literal}
|
||||
|
||||
Integer and long integer literals are described by the following
|
||||
lexical definitions:
|
||||
|
||||
\begin{verbatim}
|
||||
longinteger: integer ("l"|"L")
|
||||
integer: decimalinteger | octinteger | hexinteger
|
||||
decimalinteger: nonzerodigit digit* | "0"
|
||||
octinteger: "0" octdigit+
|
||||
hexinteger: "0" ("x"|"X") hexdigit+
|
||||
|
||||
nonzerodigit: "1"..."9"
|
||||
octdigit: "0"..."7"
|
||||
hexdigit: digit|"a"..."f"|"A"..."F"
|
||||
\end{verbatim}
|
||||
|
||||
Although both lower case `l' and upper case `L' are allowed as suffix
|
||||
for long integers, it is strongly recommended to always use `L', since
|
||||
the letter `l' looks too much like the digit `1'.
|
||||
|
||||
Plain integer decimal literals must be at most 2147483647 (i.e., the
|
||||
largest positive integer, using 32-bit arithmetic). Plain octal and
|
||||
hexadecimal literals may be as large as 4294967295, but values larger
|
||||
than 2147483647 are converted to a negative value by subtracting
|
||||
4294967296. There is no limit for long integer literals apart from
|
||||
what can be stored in available memory.
|
||||
|
||||
Some examples of plain and long integer literals:
|
||||
|
||||
\begin{verbatim}
|
||||
7 2147483647 0177 0x80000000
|
||||
3L 79228162514264337593543950336L 0377L 0x100000000L
|
||||
\end{verbatim}
|
||||
|
||||
Floating point literals are described by the following lexical
|
||||
definitions:
|
||||
|
||||
\begin{verbatim}
|
||||
floatnumber: pointfloat | exponentfloat
|
||||
pointfloat: [intpart] fraction | intpart "."
|
||||
exponentfloat: (intpart | pointfloat) exponent
|
||||
intpart: digit+
|
||||
fraction: "." digit+
|
||||
exponent: ("e"|"E") ["+"|"-"] digit+
|
||||
\end{verbatim}
|
||||
|
||||
The allowed range of floating point literals is
|
||||
implementation-dependent.
|
||||
|
||||
Some examples of floating point literals:
|
||||
|
||||
\begin{verbatim}
|
||||
3.14 10. .001 1e100 3.14e-10
|
||||
\end{verbatim}
|
||||
|
||||
Note that numeric literals do not include a sign; a phrase like
|
||||
\verb@-1@ is actually an expression composed of the operator
|
||||
\verb@-@ and the literal \verb@1@.
|
||||
|
||||
\section{Operators}
|
||||
|
||||
The following tokens are operators:
|
||||
\index{operators}
|
||||
|
||||
\begin{verbatim}
|
||||
+ - * / %
|
||||
<< >> & | ^ ~
|
||||
< == > <= <> != >=
|
||||
\end{verbatim}
|
||||
|
||||
The comparison operators \verb@<>@ and \verb@!=@ are alternate
|
||||
spellings of the same operator.
|
||||
|
||||
\section{Delimiters}
|
||||
|
||||
The following tokens serve as delimiters or otherwise have a special
|
||||
meaning:
|
||||
\index{delimiters}
|
||||
|
||||
\begin{verbatim}
|
||||
( ) [ ] { }
|
||||
, : . " ` '
|
||||
= ;
|
||||
\end{verbatim}
|
||||
|
||||
The following printing \ASCII{} characters are not used in Python. Their
|
||||
occurrence outside string literals and comments is an unconditional
|
||||
error:
|
||||
\index{ASCII}
|
||||
|
||||
\begin{verbatim}
|
||||
@ $ ?
|
||||
\end{verbatim}
|
||||
|
||||
They may be used by future versions of the language though!
|
||||
885
Doc/ref/ref3.tex
885
Doc/ref/ref3.tex
File diff suppressed because it is too large
Load Diff
201
Doc/ref/ref4.tex
201
Doc/ref/ref4.tex
@@ -1,201 +0,0 @@
|
||||
\chapter{Execution model}
|
||||
\index{execution model}
|
||||
|
||||
\section{Code blocks, execution frames, and name spaces} \label{execframes}
|
||||
\index{code block}
|
||||
\indexii{execution}{frame}
|
||||
\index{name space}
|
||||
|
||||
A {\em 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 executed only once, others
|
||||
(like function bodies) may be executed many times. Code blocks may
|
||||
textually contain other code blocks. Code blocks may invoke other
|
||||
code blocks (that may or may not be textually contained in them) as
|
||||
part of their execution, e.g. by invoking (calling) a function.
|
||||
\index{code block}
|
||||
\indexii{code}{block}
|
||||
|
||||
The following are code blocks: A module is a code block. A function
|
||||
body is a code block. A class definition is a code block. Each
|
||||
command typed interactively is a separate code block; a script file is
|
||||
a code block. The string argument passed to the built-in function
|
||||
\verb@eval@ and to the \verb@exec@ statement are code blocks.
|
||||
And finally, the
|
||||
expression read and evaluated by the built-in function \verb@input@ is
|
||||
a code block.
|
||||
|
||||
A code block is executed in an execution frame. An {\em 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
|
||||
name spaces, the local and the global name space, that affect
|
||||
execution of the code block.
|
||||
\indexii{execution}{frame}
|
||||
|
||||
A {\em name space} is a mapping from names (identifiers) to objects.
|
||||
A particular name space may be referenced by more than one execution
|
||||
frame, and from other places as well. Adding a name to a name space
|
||||
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}.
|
||||
Name spaces are functionally equivalent to dictionaries.
|
||||
\index{name space}
|
||||
\indexii{binding}{name}
|
||||
\indexii{rebinding}{name}
|
||||
\indexii{unbinding}{name}
|
||||
|
||||
The {\em local name space} of an execution frame determines the default
|
||||
place where names are defined and searched. The {\em global name
|
||||
space} determines the place where names listed in \verb@global@
|
||||
statements are defined and searched, and where names that are not
|
||||
explicitly bound in the current code block are searched.
|
||||
\indexii{local}{name space}
|
||||
\indexii{global}{name space}
|
||||
\stindex{global}
|
||||
|
||||
Whether a name is local or global in a code block is determined by
|
||||
static inspection of the source text for the code block: in the
|
||||
absence of \verb@global@ statements, a name that is bound anywhere in
|
||||
the code block is local in the entire code block; all other names are
|
||||
considered global. The \verb@global@ statement forces global
|
||||
interpretation of selected names throughout the code block. The
|
||||
following constructs bind names: formal parameters, \verb@import@
|
||||
statements, class and function definitions (these bind the class or
|
||||
function name), and targets that are identifiers if occurring in an
|
||||
assignment, \verb@for@ loop header, or \verb@except@ clause header.
|
||||
|
||||
A target occurring in a \verb@del@ statement is also considered bound
|
||||
for this purpose (though the actual semantics are to ``unbind'' the
|
||||
name).
|
||||
|
||||
When a global name is not found in the global name space, it is
|
||||
searched in the list of ``built-in'' names (which is actually the
|
||||
global name space of the module \verb@__builtin__@). When a name is not
|
||||
found at all, the \verb@NameError@ exception is raised.%
|
||||
\footnote{If the code block contains {\tt exec} statements or the
|
||||
construct {\tt from \ldots import *}, the semantics of names not
|
||||
explicitly mentioned in a {\tt global} statement change subtly: name
|
||||
lookup first searches the local name space, then the global one, then
|
||||
the built-in one.}
|
||||
\bimodindex{__builtin__}
|
||||
\stindex{from}
|
||||
\stindex{exec}
|
||||
\stindex{global}
|
||||
\ttindex{NameError}
|
||||
|
||||
The following table lists the meaning of the local and global name
|
||||
space for various types of code blocks. The name space for a
|
||||
particular module is automatically created when the module is first
|
||||
referenced. Note that in almost all cases, the global name space is
|
||||
the name space of the containing module --- scopes in Python do not
|
||||
nest!
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{|l|l|l|l|}
|
||||
\hline
|
||||
Code block type & Global name space & Local name space & Notes \\
|
||||
\hline
|
||||
Module & n.s. for this module & same as global & \\
|
||||
Script & n.s. for \verb@__main__@ & same as global & \\
|
||||
Interactive command & n.s. for \verb@__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 \verb@exec@ statement
|
||||
& global n.s. of containing block
|
||||
& local n.s. of containing block & (1) \\
|
||||
String passed to \verb@eval()@
|
||||
& global n.s. of caller & local n.s. of caller & (1) \\
|
||||
File read by \verb@execfile()@
|
||||
& global n.s. of caller & local n.s. of caller & (1) \\
|
||||
Expression read by \verb@input@
|
||||
& global n.s. of caller & local n.s. of caller & \\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
\bimodindex{__main__}
|
||||
|
||||
Notes:
|
||||
|
||||
\begin{description}
|
||||
|
||||
\item[n.s.] means {\em name space}
|
||||
|
||||
\item[(1)] The global and local name space for these can be
|
||||
overridden with optional extra arguments.
|
||||
|
||||
\item[(2)] The body of lambda forms (see section \ref{lambda}) is
|
||||
treated exactly the same as a (nested) function definition. Lambda
|
||||
forms have their own name space consisting of their formal arguments.
|
||||
\indexii{lambda}{form}
|
||||
|
||||
\end{description}
|
||||
|
||||
The built-in functions \verb@globals()@ and \verb@locals()@ returns a
|
||||
dictionary representing the current global and local name space,
|
||||
respectively. The effect of modifications to this dictionary on the
|
||||
name space are undefined.%
|
||||
\footnote{The current implementations return the dictionary actually
|
||||
used to implement the name space, {\em except} for functions, where
|
||||
the optimizer may cause the local name space to be implemented
|
||||
differently, and \verb@locals()@ returns a read-only dictionary.}
|
||||
|
||||
\section{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
|
||||
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
|
||||
by any code block that directly or indirectly invoked the code block
|
||||
where the error occurred.
|
||||
\index{exception}
|
||||
\index{raise an exception}
|
||||
\index{handle an exception}
|
||||
\index{exception handler}
|
||||
\index{errors}
|
||||
\index{error handling}
|
||||
|
||||
The Python interpreter raises an exception when it detects an run-time
|
||||
error (such as division by zero). A Python program can also
|
||||
explicitly raise an exception with the \verb@raise@ statement.
|
||||
Exception handlers are specified with the \verb@try...except@
|
||||
statement.
|
||||
|
||||
Python uses the ``termination'' model of error handling: an exception
|
||||
handler can find out what happened and continue execution at an outer
|
||||
level, but it cannot repair the cause of the error and retry the
|
||||
failing operation (except by re-entering the the offending piece of
|
||||
code from the top).
|
||||
|
||||
When an exception is not handled at all, the interpreter terminates
|
||||
execution of the program, or returns to its interactive main loop.
|
||||
|
||||
Exceptions are identified by string objects or class instances. Two
|
||||
different string objects with the same value identify different
|
||||
exceptions. An exception can be raised with a class instance. Such
|
||||
exceptions are caught by specifying an except clause that has the
|
||||
class name (or a base class) as the condition.
|
||||
|
||||
When an exception is raised, an object (maybe \verb@None@) is passed
|
||||
as the exception's ``parameter''; this object does not affect the
|
||||
selection of an exception handler, but is passed to the selected
|
||||
exception handler as additional information. For exceptions raised
|
||||
with a class instance, the instance is passed as the ``parameter''.
|
||||
|
||||
For example:
|
||||
|
||||
\begin{verbatim}
|
||||
>>> class Error:
|
||||
... def __init__(self, msg): self.msg = msg
|
||||
...
|
||||
>>> class SpecificError(Error): pass
|
||||
...
|
||||
>>> try:
|
||||
... raise SpecificError('broken')
|
||||
... except Error, obj:
|
||||
... print obj.msg
|
||||
...
|
||||
broken
|
||||
\end{verbatim}
|
||||
|
||||
See also the description of the \verb@try@ and \verb@raise@
|
||||
statements.
|
||||
759
Doc/ref/ref5.tex
759
Doc/ref/ref5.tex
File diff suppressed because it is too large
Load Diff
512
Doc/ref/ref6.tex
512
Doc/ref/ref6.tex
File diff suppressed because it is too large
Load Diff
391
Doc/ref/ref7.tex
391
Doc/ref/ref7.tex
@@ -1,391 +0,0 @@
|
||||
\chapter{Compound statements}
|
||||
\indexii{compound}{statement}
|
||||
|
||||
Compound statements contain (groups of) other statements; they affect
|
||||
or control the execution of those other statements in some way. In
|
||||
general, compound statements span multiple lines, although in simple
|
||||
incarnations a whole compound statement may be contained in one line.
|
||||
|
||||
The \verb@if@, \verb@while@ and \verb@for@ statements implement
|
||||
traditional control flow constructs. \verb@try@ specifies exception
|
||||
handlers and/or cleanup code for a group of statements. Function and
|
||||
class definitions are also syntactically compound statements.
|
||||
|
||||
Compound statements consist of one or more `clauses'. A clause
|
||||
consists of a header and a `suite'. The clause headers of a
|
||||
particular compound statement are all at the same indentation level.
|
||||
Each clause header begins with a uniquely identifying keyword and ends
|
||||
with a colon. A suite is a group of statements controlled by a
|
||||
clause. A suite can be one or more semicolon-separated simple
|
||||
statements on the same line as the header, following the header's
|
||||
colon, or it can be one or more indented statements on subsequent
|
||||
lines. Only the latter form of suite can contain nested compound
|
||||
statements; the following is illegal, mostly because it wouldn't be
|
||||
clear to which \verb@if@ clause a following \verb@else@ clause would
|
||||
belong:
|
||||
\index{clause}
|
||||
\index{suite}
|
||||
|
||||
\begin{verbatim}
|
||||
if test1: if test2: print x
|
||||
\end{verbatim}
|
||||
|
||||
Also note that the semicolon binds tighter than the colon in this
|
||||
context, so that in the following example, either all or none of the
|
||||
\verb@print@ statements are executed:
|
||||
|
||||
\begin{verbatim}
|
||||
if x < y < z: print x; print y; print z
|
||||
\end{verbatim}
|
||||
|
||||
Summarizing:
|
||||
|
||||
\begin{verbatim}
|
||||
compound_stmt: if_stmt | while_stmt | for_stmt
|
||||
| try_stmt | funcdef | classdef
|
||||
suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
|
||||
statement: stmt_list NEWLINE | compound_stmt
|
||||
stmt_list: simple_stmt (";" simple_stmt)* [";"]
|
||||
\end{verbatim}
|
||||
|
||||
Note that statements always end in a \verb@NEWLINE@ possibly followed
|
||||
by a \verb@DEDENT@.
|
||||
\index{NEWLINE token}
|
||||
\index{DEDENT token}
|
||||
|
||||
Also note that optional continuation clauses always begin with a
|
||||
keyword that cannot start a statement, thus there are no ambiguities
|
||||
(the `dangling \verb@else@' problem is solved in Python by requiring
|
||||
nested \verb@if@ statements to be indented).
|
||||
\indexii{dangling}{else}
|
||||
|
||||
The formatting of the grammar rules in the following sections places
|
||||
each clause on a separate line for clarity.
|
||||
|
||||
\section{The {\tt if} statement}
|
||||
\stindex{if}
|
||||
|
||||
The \verb@if@ statement is used for conditional execution:
|
||||
|
||||
\begin{verbatim}
|
||||
if_stmt: "if" condition ":" suite
|
||||
("elif" condition ":" suite)*
|
||||
["else" ":" suite]
|
||||
\end{verbatim}
|
||||
|
||||
It selects exactly one of the suites by evaluating the conditions one
|
||||
by one until one is found to be true (see section \ref{Booleans} for
|
||||
the definition of true and false); then that suite is executed (and no
|
||||
other part of the \verb@if@ statement is executed or evaluated). If
|
||||
all conditions are false, the suite of the \verb@else@ clause, if
|
||||
present, is executed.
|
||||
\kwindex{elif}
|
||||
\kwindex{else}
|
||||
|
||||
\section{The {\tt while} statement}
|
||||
\stindex{while}
|
||||
\indexii{loop}{statement}
|
||||
|
||||
The \verb@while@ statement is used for repeated execution as long as a
|
||||
condition is true:
|
||||
|
||||
\begin{verbatim}
|
||||
while_stmt: "while" condition ":" suite
|
||||
["else" ":" suite]
|
||||
\end{verbatim}
|
||||
|
||||
This repeatedly tests the condition and, if it is true, executes the
|
||||
first suite; if the condition is false (which may be the first time it
|
||||
is tested) the suite of the \verb@else@ clause, if present, is
|
||||
executed and the loop terminates.
|
||||
\kwindex{else}
|
||||
|
||||
A \verb@break@ statement executed in the first suite terminates the
|
||||
loop without executing the \verb@else@ clause's suite. A
|
||||
\verb@continue@ statement executed in the first suite skips the rest
|
||||
of the suite and goes back to testing the condition.
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
\section{The {\tt for} statement}
|
||||
\stindex{for}
|
||||
\indexii{loop}{statement}
|
||||
|
||||
The \verb@for@ statement is used to iterate over the elements of a
|
||||
sequence (string, tuple or list):
|
||||
\obindex{sequence}
|
||||
|
||||
\begin{verbatim}
|
||||
for_stmt: "for" target_list "in" condition_list ":" suite
|
||||
["else" ":" suite]
|
||||
\end{verbatim}
|
||||
|
||||
The condition list is evaluated once; it should yield a sequence. The
|
||||
suite is then executed once for each item in the sequence, in the
|
||||
order of ascending indices. Each item in turn is assigned to the
|
||||
target list using the standard rules for assignments, and then the
|
||||
suite is executed. When the items are exhausted (which is immediately
|
||||
when the sequence is empty), the suite in the \verb@else@ clause, if
|
||||
present, is executed, and the loop terminates.
|
||||
\kwindex{in}
|
||||
\kwindex{else}
|
||||
\indexii{target}{list}
|
||||
|
||||
A \verb@break@ statement executed in the first suite terminates the
|
||||
loop without executing the \verb@else@ clause's suite. A
|
||||
\verb@continue@ statement executed in the first suite skips the rest
|
||||
of the suite and continues with the next item, or with the \verb@else@
|
||||
clause if there was no next item.
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
The suite may assign to the variable(s) in the target list; this does
|
||||
not affect the next item assigned to it.
|
||||
|
||||
The target list is not deleted when the loop is finished, but if the
|
||||
sequence is empty, it will not have been assigned to at all by the
|
||||
loop.
|
||||
|
||||
Hint: the built-in function \verb@range()@ returns a sequence of
|
||||
integers suitable to emulate the effect of Pascal's
|
||||
\verb@for i := a to b do@;
|
||||
e.g. \verb@range(3)@ returns the list \verb@[0, 1, 2]@.
|
||||
\bifuncindex{range}
|
||||
\index{Pascal}
|
||||
|
||||
{\bf Warning:} There is a subtlety when the sequence is being modified
|
||||
by the loop (this can only occur for mutable sequences, i.e. lists).
|
||||
An internal counter is used to keep track of which item is used next,
|
||||
and this is incremented on each iteration. When this counter has
|
||||
reached the length of the sequence the loop terminates. This means that
|
||||
if the suite deletes the current (or a previous) item from the
|
||||
sequence, the next item will be skipped (since it gets the index of
|
||||
the current item which has already been treated). Likewise, if the
|
||||
suite inserts an item in the sequence before the current item, the
|
||||
current item will be treated again the next time through the loop.
|
||||
This can lead to nasty bugs that can be avoided by making a temporary
|
||||
copy using a slice of the whole sequence, e.g.
|
||||
\index{loop!over mutable sequence}
|
||||
\index{mutable sequence!loop over}
|
||||
|
||||
\begin{verbatim}
|
||||
for x in a[:]:
|
||||
if x < 0: a.remove(x)
|
||||
\end{verbatim}
|
||||
|
||||
\section{The {\tt try} statement} \label{try}
|
||||
\stindex{try}
|
||||
|
||||
The \verb@try@ statement specifies exception handlers and/or cleanup
|
||||
code for a group of statements:
|
||||
|
||||
\begin{verbatim}
|
||||
try_stmt: try_exc_stmt | try_fin_stmt
|
||||
try_exc_stmt: "try" ":" suite
|
||||
("except" [condition ["," target]] ":" suite)+
|
||||
["else" ":" suite]
|
||||
try_fin_stmt: "try" ":" suite
|
||||
"finally" ":" suite
|
||||
\end{verbatim}
|
||||
|
||||
There are two forms of \verb@try@ statement: \verb@try...except@ and
|
||||
\verb@try...finally@. These forms cannot be mixed.
|
||||
|
||||
The \verb@try...except@ form specifies one or more exception handlers
|
||||
(the \verb@except@ clauses). When no exception occurs in the
|
||||
\verb@try@ clause, no exception handler is executed. When an
|
||||
exception occurs in the \verb@try@ suite, a search for an exception
|
||||
handler is started. This inspects the except clauses in turn until
|
||||
one is found that matches the exception. A condition-less except
|
||||
clause, if present, must be last; it matches any exception. For an
|
||||
except clause with a condition, that condition is evaluated, and the
|
||||
clause matches the exception if the resulting object is ``compatible''
|
||||
with the exception. An object is compatible with an exception if it
|
||||
is either the object that identifies the exception, or (for exceptions
|
||||
that are classes) it is a base class of the exception, or it is a
|
||||
tuple containing an item that is compatible with the exception. Note
|
||||
that the object identities must match, i.e. it must be the same
|
||||
object, not just an object with the same value.
|
||||
\kwindex{except}
|
||||
|
||||
If no except clause matches the exception, the search for an exception
|
||||
handler continues in the surrounding code and on the invocation stack.
|
||||
|
||||
If the evaluation of a condition in the header of an except clause
|
||||
raises an exception, the original search for a handler is cancelled
|
||||
and a search starts for the new exception in the surrounding code and
|
||||
on the call stack (it is treated as if the entire \verb@try@ statement
|
||||
raised the exception).
|
||||
|
||||
When a matching except clause is found, the exception's parameter is
|
||||
assigned to the target specified in that except clause, if present,
|
||||
and the except clause's suite is executed. When the end of this suite
|
||||
is reached, execution continues normally after the entire try
|
||||
statement. (This means that if two nested handlers exist for the same
|
||||
exception, and the exception occurs in the try clause of the inner
|
||||
handler, the outer handler will not handle the exception.)
|
||||
|
||||
Before an except clause's suite is executed, details about the
|
||||
exception are assigned to three variables in the \verb@sys@ module:
|
||||
\verb@sys.exc_type@ receives the object identifying the exception;
|
||||
\verb@sys.exc_value@ receives the exception's parameter;
|
||||
\verb@sys.exc_traceback@ receives a traceback object (see section
|
||||
\ref{traceback}) identifying the point in the program where the
|
||||
exception occurred.
|
||||
\bimodindex{sys}
|
||||
\ttindex{exc_type}
|
||||
\ttindex{exc_value}
|
||||
\ttindex{exc_traceback}
|
||||
\obindex{traceback}
|
||||
|
||||
The optional \verb@else@ clause is executed when no exception occurs
|
||||
in the \verb@try@ clause. Exceptions in the \verb@else@ clause are
|
||||
not handled by the preceding \verb@except@ clauses.
|
||||
\kwindex{else}
|
||||
|
||||
The \verb@try...finally@ form specifies a `cleanup' handler. The
|
||||
\verb@try@ clause is executed. When no exception occurs, the
|
||||
\verb@finally@ clause is executed. When an exception occurs in the
|
||||
\verb@try@ clause, the exception is temporarily saved, the
|
||||
\verb@finally@ clause is executed, and then the saved exception is
|
||||
re-raised. If the \verb@finally@ clause raises another exception or
|
||||
executes a \verb@return@, \verb@break@ or \verb@continue@ statement,
|
||||
the saved exception is lost.
|
||||
\kwindex{finally}
|
||||
|
||||
When a \verb@return@ or \verb@break@ statement is executed in the
|
||||
\verb@try@ suite of a \verb@try...finally@ statement, the
|
||||
\verb@finally@ clause is also executed `on the way out'. A
|
||||
\verb@continue@ statement is illegal in the \verb@try@ clause. (The
|
||||
reason is a problem with the current implementation --- this
|
||||
restriction may be lifted in the future).
|
||||
\stindex{return}
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
\section{Function definitions} \label{function}
|
||||
\indexii{function}{definition}
|
||||
|
||||
A function definition defines a user-defined function object (see
|
||||
section \ref{types}):\footnote{The new syntax to receive arbitrary
|
||||
keyword arguments is not yet documented in this manual. See chapter
|
||||
12 of the Tutorial.}
|
||||
\obindex{user-defined function}
|
||||
\obindex{function}
|
||||
|
||||
\begin{verbatim}
|
||||
funcdef: "def" funcname "(" [parameter_list] ")" ":" suite
|
||||
parameter_list: (defparameter ",")* ("*" identifier [, "**" identifier]
|
||||
| "**" identifier
|
||||
| defparameter [","])
|
||||
defparameter: parameter ["=" condition]
|
||||
sublist: parameter ("," parameter)* [","]
|
||||
parameter: identifier | "(" sublist ")"
|
||||
funcname: identifier
|
||||
\end{verbatim}
|
||||
|
||||
A function definition is an executable statement. Its execution binds
|
||||
the function name in the current local name space to a function object
|
||||
(a wrapper around the executable code for the function). This
|
||||
function object contains a reference to the current global name space
|
||||
as the global name space to be used when the function is called.
|
||||
\indexii{function}{name}
|
||||
\indexii{name}{binding}
|
||||
|
||||
The function definition does not execute the function body; this gets
|
||||
executed only when the function is called.
|
||||
|
||||
When one or more top-level parameters have the form {\em parameter =
|
||||
condition}, the function is said to have ``default parameter values''.
|
||||
Default parameter values are evaluated when the function definition is
|
||||
executed. For a parameter with a default value, the correponding
|
||||
argument may be omitted from a call, in which case the parameter's
|
||||
default value is substituted. If a parameter has a default value, all
|
||||
following parameters must also have a default value --- this is a
|
||||
syntactic restriction that is not expressed by the grammar.%
|
||||
\footnote{Currently this is not checked; instead,
|
||||
{\tt def f(a=1,b)} is interpreted as {\tt def f(a=1,b=None)}.}
|
||||
\indexiii{default}{parameter}{value}
|
||||
|
||||
Function call semantics are described in section \ref{calls}. When a
|
||||
user-defined function is called, first missing arguments for which a
|
||||
default value exists are supplied; then the arguments (a.k.a. actual
|
||||
parameters) are bound to the (formal) parameters, as follows:
|
||||
\indexii{function}{call}
|
||||
\indexiii{user-defined}{function}{call}
|
||||
\index{parameter}
|
||||
\index{argument}
|
||||
\indexii{parameter}{formal}
|
||||
\indexii{parameter}{actual}
|
||||
|
||||
\begin{itemize}
|
||||
|
||||
\item
|
||||
If there are no formal parameters, there must be no arguments.
|
||||
|
||||
\item
|
||||
If the formal parameter list does not end in a star followed by an
|
||||
identifier, there must be exactly as many arguments as there are
|
||||
parameters in the formal parameter list (at the top level); the
|
||||
arguments are assigned to the formal parameters one by one. Note that
|
||||
the presence or absence of a trailing comma at the top level in either
|
||||
the formal or the actual parameter list makes no difference. The
|
||||
assignment to a formal parameter is performed as if the parameter
|
||||
occurs on the left hand side of an assignment statement whose right
|
||||
hand side's value is that of the argument.
|
||||
|
||||
\item
|
||||
If the formal parameter list ends in a star followed by an identifier,
|
||||
preceded by zero or more comma-followed parameters, there must be at
|
||||
least as many arguments as there are parameters preceding the star.
|
||||
Call this number {\em N}. The first {\em N} arguments are assigned to
|
||||
the corresponding formal parameters in the way descibed above. A
|
||||
tuple containing the remaining arguments, if any, is then assigned to
|
||||
the identifier following the star. This variable will always be a
|
||||
tuple: if there are no extra arguments, its value is \verb@()@, if
|
||||
there is just one extra argument, it is a singleton tuple.
|
||||
\indexii{variable length}{parameter list}
|
||||
|
||||
\end{itemize}
|
||||
|
||||
Note that the `variable length parameter list' feature only works at
|
||||
the top level of the parameter list; individual parameters use a model
|
||||
corresponding more closely to that of ordinary assignment. While the
|
||||
latter model is generally preferable, because of the greater type
|
||||
safety it offers (wrong-sized tuples aren't silently mistreated),
|
||||
variable length parameter lists are a sufficiently accepted practice
|
||||
in most programming languages that a compromise has been worked out.
|
||||
(And anyway, assignment has no equivalent for empty argument lists.)
|
||||
|
||||
It is also possible to create anonymous functions (functions not bound
|
||||
to a name), for immediate use in expressions. This uses lambda forms,
|
||||
described in section \ref{lambda}.
|
||||
\indexii{lambda}{form}
|
||||
|
||||
\section{Class definitions} \label{class}
|
||||
\indexii{class}{definition}
|
||||
|
||||
A class definition defines a class object (see section \ref{types}):
|
||||
\obindex{class}
|
||||
|
||||
\begin{verbatim}
|
||||
classdef: "class" classname [inheritance] ":" suite
|
||||
inheritance: "(" [condition_list] ")"
|
||||
classname: identifier
|
||||
\end{verbatim}
|
||||
|
||||
A class definition is an executable statement. It first evaluates the
|
||||
inheritance list, if present. Each item in the inheritance list
|
||||
should evaluate to a class object. The class's suite is then executed
|
||||
in a new execution frame (see section \ref{execframes}), using a newly
|
||||
created local name space and the original global name space.
|
||||
(Usually, the suite contains only function definitions.) When the
|
||||
class's suite finishes execution, its execution frame is discarded but
|
||||
its local name space is saved. A class object is then created using
|
||||
the inheritance list for the base classes and the saved local name
|
||||
space for the attribute dictionary. The class name is bound to this
|
||||
class object in the original local name space.
|
||||
\index{inheritance}
|
||||
\indexii{class}{name}
|
||||
\indexii{name}{binding}
|
||||
\indexii{execution}{frame}
|
||||
105
Doc/ref/ref8.tex
105
Doc/ref/ref8.tex
@@ -1,105 +0,0 @@
|
||||
\chapter{Top-level components}
|
||||
|
||||
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,
|
||||
typed in interactively, from a module source file, etc. This chapter
|
||||
gives the syntax used in these cases.
|
||||
\index{interpreter}
|
||||
|
||||
\section{Complete Python programs}
|
||||
\index{program}
|
||||
|
||||
While a language specification need not prescribe how the language
|
||||
interpreter is invoked, it is useful to have a notion of a complete
|
||||
Python program. A complete Python program is executed in a minimally
|
||||
initialized environment: all built-in and standard modules are
|
||||
available, but none have been initialized, except for \verb@sys@
|
||||
(various system services), \verb@__builtin__@ (built-in functions,
|
||||
exceptions and \verb@None@) and \verb@__main__@. The latter is used
|
||||
to provide the local and global name space for execution of the
|
||||
complete program.
|
||||
\bimodindex{sys}
|
||||
\bimodindex{__main__}
|
||||
\bimodindex{__builtin__}
|
||||
|
||||
The syntax for a complete Python program is that for file input,
|
||||
described in the next section.
|
||||
|
||||
The interpreter may also be invoked in interactive mode; in this case,
|
||||
it does not read and execute a complete program but reads and executes
|
||||
one statement (possibly compound) at a time. The initial environment
|
||||
is identical to that of a complete program; each statement is executed
|
||||
in the name space of \verb@__main__@.
|
||||
\index{interactive mode}
|
||||
\bimodindex{__main__}
|
||||
|
||||
Under {\UNIX}, a complete program can be passed to the interpreter in
|
||||
three forms: with the {\bf -c} {\it string} command line option, as a
|
||||
file passed as the first command line argument, or as standard input.
|
||||
If the file or standard input is a tty device, the interpreter enters
|
||||
interactive mode; otherwise, it executes the file as a complete
|
||||
program.
|
||||
\index{UNIX}
|
||||
\index{command line}
|
||||
\index{standard input}
|
||||
|
||||
\section{File input}
|
||||
|
||||
All input read from non-interactive files has the same form:
|
||||
|
||||
\begin{verbatim}
|
||||
file_input: (NEWLINE | statement)*
|
||||
\end{verbatim}
|
||||
|
||||
This syntax is used in the following situations:
|
||||
|
||||
\begin{itemize}
|
||||
|
||||
\item when parsing a complete Python program (from a file or from a string);
|
||||
|
||||
\item when parsing a module;
|
||||
|
||||
\item when parsing a string passed to the \verb@exec@ statement;
|
||||
|
||||
\end{itemize}
|
||||
|
||||
\section{Interactive input}
|
||||
|
||||
Input in interactive mode is parsed using the following grammar:
|
||||
|
||||
\begin{verbatim}
|
||||
interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE
|
||||
\end{verbatim}
|
||||
|
||||
Note that a (top-level) compound statement must be followed by a blank
|
||||
line in interactive mode; this is needed to help the parser detect the
|
||||
end of the input.
|
||||
|
||||
\section{Expression input}
|
||||
\index{input}
|
||||
|
||||
There are two forms of expression input. Both ignore leading
|
||||
whitespace.
|
||||
|
||||
The string argument to \verb@eval()@ must have the following form:
|
||||
\bifuncindex{eval}
|
||||
|
||||
\begin{verbatim}
|
||||
eval_input: condition_list NEWLINE*
|
||||
\end{verbatim}
|
||||
|
||||
The input line read by \verb@input()@ must have the following form:
|
||||
\bifuncindex{input}
|
||||
|
||||
\begin{verbatim}
|
||||
input_input: condition_list NEWLINE
|
||||
\end{verbatim}
|
||||
|
||||
Note: to read `raw' input line without interpretation, you can use the
|
||||
built-in function \verb@raw_input()@ or the \verb@readline()@ method
|
||||
of file objects.
|
||||
\obindex{file}
|
||||
\index{input!raw}
|
||||
\index{raw input}
|
||||
\bifuncindex{raw_index}
|
||||
\ttindex{readline}
|
||||
81
Doc/ref1.tex
81
Doc/ref1.tex
@@ -1,81 +0,0 @@
|
||||
\chapter{Introduction}
|
||||
|
||||
This reference manual describes the Python programming language.
|
||||
It is not intended as a tutorial.
|
||||
|
||||
While I am trying to be as precise as possible, I chose to use English
|
||||
rather than formal specifications for everything except syntax and
|
||||
lexical analysis. This should make the document more understandable
|
||||
to the average reader, but will leave room for ambiguities.
|
||||
Consequently, if you were coming from Mars and tried to re-implement
|
||||
Python from this document alone, you might have to guess things and in
|
||||
fact you would probably end up implementing quite a different language.
|
||||
On the other hand, if you are using
|
||||
Python and wonder what the precise rules about a particular area of
|
||||
the language are, you should definitely be able to find them here.
|
||||
|
||||
It is dangerous to add too many implementation details to a language
|
||||
reference document --- the implementation may change, and other
|
||||
implementations of the same language may work differently. On the
|
||||
other hand, there is currently only one Python implementation, and
|
||||
its particular quirks are sometimes worth being mentioned, especially
|
||||
where the implementation imposes additional limitations. Therefore,
|
||||
you'll find short ``implementation notes'' sprinkled throughout the
|
||||
text.
|
||||
|
||||
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
|
||||
mentioned when they interact in a significant way with the language
|
||||
definition.
|
||||
|
||||
\section{Notation}
|
||||
|
||||
The descriptions of lexical analysis and syntax use a modified BNF
|
||||
grammar notation. This uses the following style of definition:
|
||||
\index{BNF}
|
||||
\index{grammar}
|
||||
\index{syntax}
|
||||
\index{notation}
|
||||
|
||||
\begin{verbatim}
|
||||
name: lc_letter (lc_letter | "_")*
|
||||
lc_letter: "a"..."z"
|
||||
\end{verbatim}
|
||||
|
||||
The first line says that a \verb@name@ is an \verb@lc_letter@ followed by
|
||||
a sequence of zero or more \verb@lc_letter@s and underscores. An
|
||||
\verb@lc_letter@ in turn is any of the single characters `a' through `z'.
|
||||
(This rule is actually adhered to for the names defined in lexical and
|
||||
grammar rules in this document.)
|
||||
|
||||
Each rule begins with a name (which is the name defined by the rule)
|
||||
and a colon. A vertical bar (\verb@|@) is used to separate
|
||||
alternatives; it is the least binding operator in this notation. A
|
||||
star (\verb@*@) means zero or more repetitions of the preceding item;
|
||||
likewise, a plus (\verb@+@) means one or more repetitions, and a
|
||||
phrase enclosed in square brackets (\verb@[ ]@) means zero or one
|
||||
occurrences (in other words, the enclosed phrase is optional). The
|
||||
\verb@*@ and \verb@+@ operators bind as tightly as possible;
|
||||
parentheses are used for grouping. Literal strings are enclosed in
|
||||
quotes. White space is only meaningful to separate tokens.
|
||||
Rules are normally contained on a single line; rules with many
|
||||
alternatives may be formatted alternatively with each line after the
|
||||
first beginning with a vertical bar.
|
||||
|
||||
In lexical definitions (as the example above), two more conventions
|
||||
are used: Two literal characters separated by three dots mean a choice
|
||||
of any single character in the given (inclusive) range of \ASCII{}
|
||||
characters. A phrase between angular brackets (\verb@<...>@) gives an
|
||||
informal description of the symbol defined; e.g. this could be used
|
||||
to describe the notion of `control character' if needed.
|
||||
\index{lexical definitions}
|
||||
\index{ASCII}
|
||||
|
||||
Even though the notation used is almost the same, there is a big
|
||||
difference between the meaning of lexical and syntactic definitions:
|
||||
a lexical definition operates on the individual characters of the
|
||||
input source, while a syntax definition operates on the stream of
|
||||
tokens generated by the lexical analysis. All uses of BNF in the next
|
||||
chapter (``Lexical Analysis'') are lexical definitions; uses in
|
||||
subsequent chapters are syntactic definitions.
|
||||
372
Doc/ref2.tex
372
Doc/ref2.tex
@@ -1,372 +0,0 @@
|
||||
\chapter{Lexical analysis}
|
||||
|
||||
A Python program is read by a {\em parser}. Input to the parser is a
|
||||
stream of {\em tokens}, generated by the {\em lexical analyzer}. This
|
||||
chapter describes how the lexical analyzer breaks a file into tokens.
|
||||
\index{lexical analysis}
|
||||
\index{parser}
|
||||
\index{token}
|
||||
|
||||
\section{Line structure}
|
||||
|
||||
A Python program is divided in a number of logical lines. The end of
|
||||
a logical line is represented by the token NEWLINE. Statements cannot
|
||||
cross logical line boundaries except where NEWLINE is allowed by the
|
||||
syntax (e.g. between statements in compound statements).
|
||||
\index{line structure}
|
||||
\index{logical line}
|
||||
\index{NEWLINE token}
|
||||
|
||||
\subsection{Comments}
|
||||
|
||||
A comment starts with a hash character (\verb@#@) that is not part of
|
||||
a string literal, and ends at the end of the physical line. A comment
|
||||
always signifies the end of the logical line. Comments are ignored by
|
||||
the syntax.
|
||||
\index{comment}
|
||||
\index{logical line}
|
||||
\index{physical line}
|
||||
\index{hash character}
|
||||
|
||||
\subsection{Explicit line joining}
|
||||
|
||||
Two or more physical lines may be joined into logical lines using
|
||||
backslash characters (\verb/\/), as follows: when a physical line ends
|
||||
in a backslash that is not part of a string literal or comment, it is
|
||||
joined with the following forming a single logical line, deleting the
|
||||
backslash and the following end-of-line character. For example:
|
||||
\index{physical line}
|
||||
\index{line joining}
|
||||
\index{line continuation}
|
||||
\index{backslash character}
|
||||
%
|
||||
\begin{verbatim}
|
||||
if 1900 < year < 2100 and 1 <= month <= 12 \
|
||||
and 1 <= day <= 31 and 0 <= hour < 24 \
|
||||
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
|
||||
return 1
|
||||
\end{verbatim}
|
||||
|
||||
A line ending in a backslash cannot carry a comment; a backslash does
|
||||
not continue a comment (but it does continue a string literal, see
|
||||
below).
|
||||
|
||||
\subsection{Implicit line joining}
|
||||
|
||||
Expressions in parentheses, square brackets or curly braces can be
|
||||
split over more than one physical line without using backslashes.
|
||||
For example:
|
||||
|
||||
\begin{verbatim}
|
||||
month_names = ['Januari', 'Februari', 'Maart', # These are the
|
||||
'April', 'Mei', 'Juni', # Dutch names
|
||||
'Juli', 'Augustus', 'September', # for the months
|
||||
'Oktober', 'November', 'December'] # of the year
|
||||
\end{verbatim}
|
||||
|
||||
Implicitly continued lines can carry comments. The indentation of the
|
||||
continuation lines is not important. Blank continuation lines are
|
||||
allowed.
|
||||
|
||||
\subsection{Blank lines}
|
||||
|
||||
A logical line that contains only spaces, tabs, and possibly a
|
||||
comment, is ignored (i.e., no NEWLINE token is generated), except that
|
||||
during interactive input of statements, an entirely blank logical line
|
||||
terminates a multi-line statement.
|
||||
\index{blank line}
|
||||
|
||||
\subsection{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
|
||||
turn is used to determine the grouping of statements.
|
||||
\index{indentation}
|
||||
\index{whitespace}
|
||||
\index{leading whitespace}
|
||||
\index{space}
|
||||
\index{tab}
|
||||
\index{grouping}
|
||||
\index{statement grouping}
|
||||
|
||||
First, tabs are replaced (from left to right) by one to eight spaces
|
||||
such that the total number of characters up to there is a multiple of
|
||||
eight (this is intended to be the same rule as used by {\UNIX}). The
|
||||
total number of spaces preceding the first non-blank character then
|
||||
determines the line's indentation. Indentation cannot be split over
|
||||
multiple physical lines using backslashes.
|
||||
|
||||
The indentation levels of consecutive lines are used to generate
|
||||
INDENT and DEDENT tokens, using a stack, as follows.
|
||||
\index{INDENT token}
|
||||
\index{DEDENT token}
|
||||
|
||||
Before the first line of the file is read, a single zero is pushed on
|
||||
the stack; this will never be popped off again. The numbers pushed on
|
||||
the stack will always be strictly increasing from bottom to top. At
|
||||
the beginning of each logical line, the line's indentation level is
|
||||
compared to the top of the stack. If it is equal, nothing happens.
|
||||
If it is larger, it is pushed on the stack, and one INDENT token is
|
||||
generated. If it is smaller, it {\em must} be one of the numbers
|
||||
occurring on the stack; all numbers on the stack that are larger are
|
||||
popped off, and for each number popped off a DEDENT token is
|
||||
generated. At the end of the file, a DEDENT token is generated for
|
||||
each number remaining on the stack that is larger than zero.
|
||||
|
||||
Here is an example of a correctly (though confusingly) indented piece
|
||||
of Python code:
|
||||
|
||||
\begin{verbatim}
|
||||
def perm(l):
|
||||
# Compute the list of all permutations of l
|
||||
|
||||
if len(l) <= 1:
|
||||
return [l]
|
||||
r = []
|
||||
for i in range(len(l)):
|
||||
s = l[:i] + l[i+1:]
|
||||
p = perm(s)
|
||||
for x in p:
|
||||
r.append(l[i:i+1] + x)
|
||||
return r
|
||||
\end{verbatim}
|
||||
|
||||
The following example shows various indentation errors:
|
||||
|
||||
\begin{verbatim}
|
||||
def perm(l): # error: first line indented
|
||||
for i in range(len(l)): # error: not indented
|
||||
s = l[:i] + l[i+1:]
|
||||
p = perm(l[:i] + l[i+1:]) # error: unexpected indent
|
||||
for x in p:
|
||||
r.append(l[i:i+1] + x)
|
||||
return r # error: inconsistent dedent
|
||||
\end{verbatim}
|
||||
|
||||
(Actually, the first three errors are detected by the parser; only the
|
||||
last error is found by the lexical analyzer --- the indentation of
|
||||
\verb@return r@ does not match a level popped off the stack.)
|
||||
|
||||
\section{Other tokens}
|
||||
|
||||
Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
|
||||
exist: identifiers, keywords, literals, operators, and delimiters.
|
||||
Spaces and tabs are not tokens, but serve to delimit tokens. Where
|
||||
ambiguity exists, a token comprises the longest possible string that
|
||||
forms a legal token, when read from left to right.
|
||||
|
||||
\section{Identifiers}
|
||||
|
||||
Identifiers (also referred to as names) are described by the following
|
||||
lexical definitions:
|
||||
\index{identifier}
|
||||
\index{name}
|
||||
|
||||
\begin{verbatim}
|
||||
identifier: (letter|"_") (letter|digit|"_")*
|
||||
letter: lowercase | uppercase
|
||||
lowercase: "a"..."z"
|
||||
uppercase: "A"..."Z"
|
||||
digit: "0"..."9"
|
||||
\end{verbatim}
|
||||
|
||||
Identifiers are unlimited in length. Case is significant.
|
||||
|
||||
\subsection{Keywords}
|
||||
|
||||
The following identifiers are used as reserved words, or {\em
|
||||
keywords} of the language, and cannot be used as ordinary
|
||||
identifiers. They must be spelled exactly as written here:
|
||||
\index{keyword}
|
||||
\index{reserved word}
|
||||
|
||||
\begin{verbatim}
|
||||
and elif global not try
|
||||
break else if or while
|
||||
class except import pass
|
||||
continue finally in print
|
||||
def for is raise
|
||||
del from lambda return
|
||||
\end{verbatim}
|
||||
|
||||
% When adding keywords, pipe it through keywords.py for reformatting
|
||||
|
||||
\section{Literals} \label{literals}
|
||||
|
||||
Literals are notations for constant values of some built-in types.
|
||||
\index{literal}
|
||||
\index{constant}
|
||||
|
||||
\subsection{String literals}
|
||||
|
||||
String literals are described by the following lexical definitions:
|
||||
\index{string literal}
|
||||
|
||||
\begin{verbatim}
|
||||
stringliteral: shortstring | longstring
|
||||
shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
|
||||
longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
|
||||
shortstringitem: shortstringchar | escapeseq
|
||||
longstringitem: longstringchar | escapeseq
|
||||
shortstringchar: <any ASCII character except "\" or newline or the quote>
|
||||
longstringchar: <any ASCII character except "\">
|
||||
escapeseq: "\" <any ASCII character>
|
||||
\end{verbatim}
|
||||
\index{ASCII}
|
||||
|
||||
In ``long strings'' (strings surrounded by sets of three quotes),
|
||||
unescaped newlines and quotes are allowed (and are retained), except
|
||||
that three unescaped quotes in a row terminate the string. (A
|
||||
``quote'' is the character used to open the string, i.e. either
|
||||
\verb/'/ or \verb/"/.)
|
||||
|
||||
Escape sequences in strings are interpreted according to rules similar
|
||||
to those used by Standard C. The recognized escape sequences are:
|
||||
\index{physical line}
|
||||
\index{escape sequence}
|
||||
\index{Standard C}
|
||||
\index{C}
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{|l|l|}
|
||||
\hline
|
||||
\verb/\/{\em newline} & Ignored \\
|
||||
\verb/\\/ & Backslash (\verb/\/) \\
|
||||
\verb/\'/ & Single quote (\verb/'/) \\
|
||||
\verb/\"/ & Double quote (\verb/"/) \\
|
||||
\verb/\a/ & \ASCII{} Bell (BEL) \\
|
||||
\verb/\b/ & \ASCII{} Backspace (BS) \\
|
||||
%\verb/\E/ & \ASCII{} Escape (ESC) \\
|
||||
\verb/\f/ & \ASCII{} Formfeed (FF) \\
|
||||
\verb/\n/ & \ASCII{} Linefeed (LF) \\
|
||||
\verb/\r/ & \ASCII{} Carriage Return (CR) \\
|
||||
\verb/\t/ & \ASCII{} Horizontal Tab (TAB) \\
|
||||
\verb/\v/ & \ASCII{} Vertical Tab (VT) \\
|
||||
\verb/\/{\em ooo} & \ASCII{} character with octal value {\em ooo} \\
|
||||
\verb/\x/{\em xx...} & \ASCII{} character with hex value {\em xx...} \\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
\index{ASCII}
|
||||
|
||||
In strict compatibility with Standard C, up to three octal digits are
|
||||
accepted, but an unlimited number of hex digits is taken to be part of
|
||||
the hex escape (and then the lower 8 bits of the resulting hex number
|
||||
are used in all current implementations...).
|
||||
|
||||
All unrecognized escape sequences are left in the string unchanged,
|
||||
i.e., {\em the backslash is left in the string.} (This behavior is
|
||||
useful when debugging: if an escape sequence is mistyped, the
|
||||
resulting output is more easily recognized as broken. It also helps a
|
||||
great deal for string literals used as regular expressions or
|
||||
otherwise passed to other modules that do their own escape handling.)
|
||||
\index{unrecognized escape sequence}
|
||||
|
||||
\subsection{Numeric literals}
|
||||
|
||||
There are three types of numeric literals: plain integers, long
|
||||
integers, and floating point numbers.
|
||||
\index{number}
|
||||
\index{numeric literal}
|
||||
\index{integer literal}
|
||||
\index{plain integer literal}
|
||||
\index{long integer literal}
|
||||
\index{floating point literal}
|
||||
\index{hexadecimal literal}
|
||||
\index{octal literal}
|
||||
\index{decimal literal}
|
||||
|
||||
Integer and long integer literals are described by the following
|
||||
lexical definitions:
|
||||
|
||||
\begin{verbatim}
|
||||
longinteger: integer ("l"|"L")
|
||||
integer: decimalinteger | octinteger | hexinteger
|
||||
decimalinteger: nonzerodigit digit* | "0"
|
||||
octinteger: "0" octdigit+
|
||||
hexinteger: "0" ("x"|"X") hexdigit+
|
||||
|
||||
nonzerodigit: "1"..."9"
|
||||
octdigit: "0"..."7"
|
||||
hexdigit: digit|"a"..."f"|"A"..."F"
|
||||
\end{verbatim}
|
||||
|
||||
Although both lower case `l' and upper case `L' are allowed as suffix
|
||||
for long integers, it is strongly recommended to always use `L', since
|
||||
the letter `l' looks too much like the digit `1'.
|
||||
|
||||
Plain integer decimal literals must be at most 2147483647 (i.e., the
|
||||
largest positive integer, using 32-bit arithmetic). Plain octal and
|
||||
hexadecimal literals may be as large as 4294967295, but values larger
|
||||
than 2147483647 are converted to a negative value by subtracting
|
||||
4294967296. There is no limit for long integer literals apart from
|
||||
what can be stored in available memory.
|
||||
|
||||
Some examples of plain and long integer literals:
|
||||
|
||||
\begin{verbatim}
|
||||
7 2147483647 0177 0x80000000
|
||||
3L 79228162514264337593543950336L 0377L 0x100000000L
|
||||
\end{verbatim}
|
||||
|
||||
Floating point literals are described by the following lexical
|
||||
definitions:
|
||||
|
||||
\begin{verbatim}
|
||||
floatnumber: pointfloat | exponentfloat
|
||||
pointfloat: [intpart] fraction | intpart "."
|
||||
exponentfloat: (intpart | pointfloat) exponent
|
||||
intpart: digit+
|
||||
fraction: "." digit+
|
||||
exponent: ("e"|"E") ["+"|"-"] digit+
|
||||
\end{verbatim}
|
||||
|
||||
The allowed range of floating point literals is
|
||||
implementation-dependent.
|
||||
|
||||
Some examples of floating point literals:
|
||||
|
||||
\begin{verbatim}
|
||||
3.14 10. .001 1e100 3.14e-10
|
||||
\end{verbatim}
|
||||
|
||||
Note that numeric literals do not include a sign; a phrase like
|
||||
\verb@-1@ is actually an expression composed of the operator
|
||||
\verb@-@ and the literal \verb@1@.
|
||||
|
||||
\section{Operators}
|
||||
|
||||
The following tokens are operators:
|
||||
\index{operators}
|
||||
|
||||
\begin{verbatim}
|
||||
+ - * / %
|
||||
<< >> & | ^ ~
|
||||
< == > <= <> != >=
|
||||
\end{verbatim}
|
||||
|
||||
The comparison operators \verb@<>@ and \verb@!=@ are alternate
|
||||
spellings of the same operator.
|
||||
|
||||
\section{Delimiters}
|
||||
|
||||
The following tokens serve as delimiters or otherwise have a special
|
||||
meaning:
|
||||
\index{delimiters}
|
||||
|
||||
\begin{verbatim}
|
||||
( ) [ ] { }
|
||||
, : . " ` '
|
||||
= ;
|
||||
\end{verbatim}
|
||||
|
||||
The following printing \ASCII{} characters are not used in Python. Their
|
||||
occurrence outside string literals and comments is an unconditional
|
||||
error:
|
||||
\index{ASCII}
|
||||
|
||||
\begin{verbatim}
|
||||
@ $ ?
|
||||
\end{verbatim}
|
||||
|
||||
They may be used by future versions of the language though!
|
||||
885
Doc/ref3.tex
885
Doc/ref3.tex
File diff suppressed because it is too large
Load Diff
201
Doc/ref4.tex
201
Doc/ref4.tex
@@ -1,201 +0,0 @@
|
||||
\chapter{Execution model}
|
||||
\index{execution model}
|
||||
|
||||
\section{Code blocks, execution frames, and name spaces} \label{execframes}
|
||||
\index{code block}
|
||||
\indexii{execution}{frame}
|
||||
\index{name space}
|
||||
|
||||
A {\em 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 executed only once, others
|
||||
(like function bodies) may be executed many times. Code blocks may
|
||||
textually contain other code blocks. Code blocks may invoke other
|
||||
code blocks (that may or may not be textually contained in them) as
|
||||
part of their execution, e.g. by invoking (calling) a function.
|
||||
\index{code block}
|
||||
\indexii{code}{block}
|
||||
|
||||
The following are code blocks: A module is a code block. A function
|
||||
body is a code block. A class definition is a code block. Each
|
||||
command typed interactively is a separate code block; a script file is
|
||||
a code block. The string argument passed to the built-in function
|
||||
\verb@eval@ and to the \verb@exec@ statement are code blocks.
|
||||
And finally, the
|
||||
expression read and evaluated by the built-in function \verb@input@ is
|
||||
a code block.
|
||||
|
||||
A code block is executed in an execution frame. An {\em 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
|
||||
name spaces, the local and the global name space, that affect
|
||||
execution of the code block.
|
||||
\indexii{execution}{frame}
|
||||
|
||||
A {\em name space} is a mapping from names (identifiers) to objects.
|
||||
A particular name space may be referenced by more than one execution
|
||||
frame, and from other places as well. Adding a name to a name space
|
||||
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}.
|
||||
Name spaces are functionally equivalent to dictionaries.
|
||||
\index{name space}
|
||||
\indexii{binding}{name}
|
||||
\indexii{rebinding}{name}
|
||||
\indexii{unbinding}{name}
|
||||
|
||||
The {\em local name space} of an execution frame determines the default
|
||||
place where names are defined and searched. The {\em global name
|
||||
space} determines the place where names listed in \verb@global@
|
||||
statements are defined and searched, and where names that are not
|
||||
explicitly bound in the current code block are searched.
|
||||
\indexii{local}{name space}
|
||||
\indexii{global}{name space}
|
||||
\stindex{global}
|
||||
|
||||
Whether a name is local or global in a code block is determined by
|
||||
static inspection of the source text for the code block: in the
|
||||
absence of \verb@global@ statements, a name that is bound anywhere in
|
||||
the code block is local in the entire code block; all other names are
|
||||
considered global. The \verb@global@ statement forces global
|
||||
interpretation of selected names throughout the code block. The
|
||||
following constructs bind names: formal parameters, \verb@import@
|
||||
statements, class and function definitions (these bind the class or
|
||||
function name), and targets that are identifiers if occurring in an
|
||||
assignment, \verb@for@ loop header, or \verb@except@ clause header.
|
||||
|
||||
A target occurring in a \verb@del@ statement is also considered bound
|
||||
for this purpose (though the actual semantics are to ``unbind'' the
|
||||
name).
|
||||
|
||||
When a global name is not found in the global name space, it is
|
||||
searched in the list of ``built-in'' names (which is actually the
|
||||
global name space of the module \verb@__builtin__@). When a name is not
|
||||
found at all, the \verb@NameError@ exception is raised.%
|
||||
\footnote{If the code block contains {\tt exec} statements or the
|
||||
construct {\tt from \ldots import *}, the semantics of names not
|
||||
explicitly mentioned in a {\tt global} statement change subtly: name
|
||||
lookup first searches the local name space, then the global one, then
|
||||
the built-in one.}
|
||||
\bimodindex{__builtin__}
|
||||
\stindex{from}
|
||||
\stindex{exec}
|
||||
\stindex{global}
|
||||
\ttindex{NameError}
|
||||
|
||||
The following table lists the meaning of the local and global name
|
||||
space for various types of code blocks. The name space for a
|
||||
particular module is automatically created when the module is first
|
||||
referenced. Note that in almost all cases, the global name space is
|
||||
the name space of the containing module --- scopes in Python do not
|
||||
nest!
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{|l|l|l|l|}
|
||||
\hline
|
||||
Code block type & Global name space & Local name space & Notes \\
|
||||
\hline
|
||||
Module & n.s. for this module & same as global & \\
|
||||
Script & n.s. for \verb@__main__@ & same as global & \\
|
||||
Interactive command & n.s. for \verb@__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 \verb@exec@ statement
|
||||
& global n.s. of containing block
|
||||
& local n.s. of containing block & (1) \\
|
||||
String passed to \verb@eval()@
|
||||
& global n.s. of caller & local n.s. of caller & (1) \\
|
||||
File read by \verb@execfile()@
|
||||
& global n.s. of caller & local n.s. of caller & (1) \\
|
||||
Expression read by \verb@input@
|
||||
& global n.s. of caller & local n.s. of caller & \\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
\bimodindex{__main__}
|
||||
|
||||
Notes:
|
||||
|
||||
\begin{description}
|
||||
|
||||
\item[n.s.] means {\em name space}
|
||||
|
||||
\item[(1)] The global and local name space for these can be
|
||||
overridden with optional extra arguments.
|
||||
|
||||
\item[(2)] The body of lambda forms (see section \ref{lambda}) is
|
||||
treated exactly the same as a (nested) function definition. Lambda
|
||||
forms have their own name space consisting of their formal arguments.
|
||||
\indexii{lambda}{form}
|
||||
|
||||
\end{description}
|
||||
|
||||
The built-in functions \verb@globals()@ and \verb@locals()@ returns a
|
||||
dictionary representing the current global and local name space,
|
||||
respectively. The effect of modifications to this dictionary on the
|
||||
name space are undefined.%
|
||||
\footnote{The current implementations return the dictionary actually
|
||||
used to implement the name space, {\em except} for functions, where
|
||||
the optimizer may cause the local name space to be implemented
|
||||
differently, and \verb@locals()@ returns a read-only dictionary.}
|
||||
|
||||
\section{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
|
||||
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
|
||||
by any code block that directly or indirectly invoked the code block
|
||||
where the error occurred.
|
||||
\index{exception}
|
||||
\index{raise an exception}
|
||||
\index{handle an exception}
|
||||
\index{exception handler}
|
||||
\index{errors}
|
||||
\index{error handling}
|
||||
|
||||
The Python interpreter raises an exception when it detects an run-time
|
||||
error (such as division by zero). A Python program can also
|
||||
explicitly raise an exception with the \verb@raise@ statement.
|
||||
Exception handlers are specified with the \verb@try...except@
|
||||
statement.
|
||||
|
||||
Python uses the ``termination'' model of error handling: an exception
|
||||
handler can find out what happened and continue execution at an outer
|
||||
level, but it cannot repair the cause of the error and retry the
|
||||
failing operation (except by re-entering the the offending piece of
|
||||
code from the top).
|
||||
|
||||
When an exception is not handled at all, the interpreter terminates
|
||||
execution of the program, or returns to its interactive main loop.
|
||||
|
||||
Exceptions are identified by string objects or class instances. Two
|
||||
different string objects with the same value identify different
|
||||
exceptions. An exception can be raised with a class instance. Such
|
||||
exceptions are caught by specifying an except clause that has the
|
||||
class name (or a base class) as the condition.
|
||||
|
||||
When an exception is raised, an object (maybe \verb@None@) is passed
|
||||
as the exception's ``parameter''; this object does not affect the
|
||||
selection of an exception handler, but is passed to the selected
|
||||
exception handler as additional information. For exceptions raised
|
||||
with a class instance, the instance is passed as the ``parameter''.
|
||||
|
||||
For example:
|
||||
|
||||
\begin{verbatim}
|
||||
>>> class Error:
|
||||
... def __init__(self, msg): self.msg = msg
|
||||
...
|
||||
>>> class SpecificError(Error): pass
|
||||
...
|
||||
>>> try:
|
||||
... raise SpecificError('broken')
|
||||
... except Error, obj:
|
||||
... print obj.msg
|
||||
...
|
||||
broken
|
||||
\end{verbatim}
|
||||
|
||||
See also the description of the \verb@try@ and \verb@raise@
|
||||
statements.
|
||||
759
Doc/ref5.tex
759
Doc/ref5.tex
File diff suppressed because it is too large
Load Diff
512
Doc/ref6.tex
512
Doc/ref6.tex
File diff suppressed because it is too large
Load Diff
391
Doc/ref7.tex
391
Doc/ref7.tex
@@ -1,391 +0,0 @@
|
||||
\chapter{Compound statements}
|
||||
\indexii{compound}{statement}
|
||||
|
||||
Compound statements contain (groups of) other statements; they affect
|
||||
or control the execution of those other statements in some way. In
|
||||
general, compound statements span multiple lines, although in simple
|
||||
incarnations a whole compound statement may be contained in one line.
|
||||
|
||||
The \verb@if@, \verb@while@ and \verb@for@ statements implement
|
||||
traditional control flow constructs. \verb@try@ specifies exception
|
||||
handlers and/or cleanup code for a group of statements. Function and
|
||||
class definitions are also syntactically compound statements.
|
||||
|
||||
Compound statements consist of one or more `clauses'. A clause
|
||||
consists of a header and a `suite'. The clause headers of a
|
||||
particular compound statement are all at the same indentation level.
|
||||
Each clause header begins with a uniquely identifying keyword and ends
|
||||
with a colon. A suite is a group of statements controlled by a
|
||||
clause. A suite can be one or more semicolon-separated simple
|
||||
statements on the same line as the header, following the header's
|
||||
colon, or it can be one or more indented statements on subsequent
|
||||
lines. Only the latter form of suite can contain nested compound
|
||||
statements; the following is illegal, mostly because it wouldn't be
|
||||
clear to which \verb@if@ clause a following \verb@else@ clause would
|
||||
belong:
|
||||
\index{clause}
|
||||
\index{suite}
|
||||
|
||||
\begin{verbatim}
|
||||
if test1: if test2: print x
|
||||
\end{verbatim}
|
||||
|
||||
Also note that the semicolon binds tighter than the colon in this
|
||||
context, so that in the following example, either all or none of the
|
||||
\verb@print@ statements are executed:
|
||||
|
||||
\begin{verbatim}
|
||||
if x < y < z: print x; print y; print z
|
||||
\end{verbatim}
|
||||
|
||||
Summarizing:
|
||||
|
||||
\begin{verbatim}
|
||||
compound_stmt: if_stmt | while_stmt | for_stmt
|
||||
| try_stmt | funcdef | classdef
|
||||
suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
|
||||
statement: stmt_list NEWLINE | compound_stmt
|
||||
stmt_list: simple_stmt (";" simple_stmt)* [";"]
|
||||
\end{verbatim}
|
||||
|
||||
Note that statements always end in a \verb@NEWLINE@ possibly followed
|
||||
by a \verb@DEDENT@.
|
||||
\index{NEWLINE token}
|
||||
\index{DEDENT token}
|
||||
|
||||
Also note that optional continuation clauses always begin with a
|
||||
keyword that cannot start a statement, thus there are no ambiguities
|
||||
(the `dangling \verb@else@' problem is solved in Python by requiring
|
||||
nested \verb@if@ statements to be indented).
|
||||
\indexii{dangling}{else}
|
||||
|
||||
The formatting of the grammar rules in the following sections places
|
||||
each clause on a separate line for clarity.
|
||||
|
||||
\section{The {\tt if} statement}
|
||||
\stindex{if}
|
||||
|
||||
The \verb@if@ statement is used for conditional execution:
|
||||
|
||||
\begin{verbatim}
|
||||
if_stmt: "if" condition ":" suite
|
||||
("elif" condition ":" suite)*
|
||||
["else" ":" suite]
|
||||
\end{verbatim}
|
||||
|
||||
It selects exactly one of the suites by evaluating the conditions one
|
||||
by one until one is found to be true (see section \ref{Booleans} for
|
||||
the definition of true and false); then that suite is executed (and no
|
||||
other part of the \verb@if@ statement is executed or evaluated). If
|
||||
all conditions are false, the suite of the \verb@else@ clause, if
|
||||
present, is executed.
|
||||
\kwindex{elif}
|
||||
\kwindex{else}
|
||||
|
||||
\section{The {\tt while} statement}
|
||||
\stindex{while}
|
||||
\indexii{loop}{statement}
|
||||
|
||||
The \verb@while@ statement is used for repeated execution as long as a
|
||||
condition is true:
|
||||
|
||||
\begin{verbatim}
|
||||
while_stmt: "while" condition ":" suite
|
||||
["else" ":" suite]
|
||||
\end{verbatim}
|
||||
|
||||
This repeatedly tests the condition and, if it is true, executes the
|
||||
first suite; if the condition is false (which may be the first time it
|
||||
is tested) the suite of the \verb@else@ clause, if present, is
|
||||
executed and the loop terminates.
|
||||
\kwindex{else}
|
||||
|
||||
A \verb@break@ statement executed in the first suite terminates the
|
||||
loop without executing the \verb@else@ clause's suite. A
|
||||
\verb@continue@ statement executed in the first suite skips the rest
|
||||
of the suite and goes back to testing the condition.
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
\section{The {\tt for} statement}
|
||||
\stindex{for}
|
||||
\indexii{loop}{statement}
|
||||
|
||||
The \verb@for@ statement is used to iterate over the elements of a
|
||||
sequence (string, tuple or list):
|
||||
\obindex{sequence}
|
||||
|
||||
\begin{verbatim}
|
||||
for_stmt: "for" target_list "in" condition_list ":" suite
|
||||
["else" ":" suite]
|
||||
\end{verbatim}
|
||||
|
||||
The condition list is evaluated once; it should yield a sequence. The
|
||||
suite is then executed once for each item in the sequence, in the
|
||||
order of ascending indices. Each item in turn is assigned to the
|
||||
target list using the standard rules for assignments, and then the
|
||||
suite is executed. When the items are exhausted (which is immediately
|
||||
when the sequence is empty), the suite in the \verb@else@ clause, if
|
||||
present, is executed, and the loop terminates.
|
||||
\kwindex{in}
|
||||
\kwindex{else}
|
||||
\indexii{target}{list}
|
||||
|
||||
A \verb@break@ statement executed in the first suite terminates the
|
||||
loop without executing the \verb@else@ clause's suite. A
|
||||
\verb@continue@ statement executed in the first suite skips the rest
|
||||
of the suite and continues with the next item, or with the \verb@else@
|
||||
clause if there was no next item.
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
The suite may assign to the variable(s) in the target list; this does
|
||||
not affect the next item assigned to it.
|
||||
|
||||
The target list is not deleted when the loop is finished, but if the
|
||||
sequence is empty, it will not have been assigned to at all by the
|
||||
loop.
|
||||
|
||||
Hint: the built-in function \verb@range()@ returns a sequence of
|
||||
integers suitable to emulate the effect of Pascal's
|
||||
\verb@for i := a to b do@;
|
||||
e.g. \verb@range(3)@ returns the list \verb@[0, 1, 2]@.
|
||||
\bifuncindex{range}
|
||||
\index{Pascal}
|
||||
|
||||
{\bf Warning:} There is a subtlety when the sequence is being modified
|
||||
by the loop (this can only occur for mutable sequences, i.e. lists).
|
||||
An internal counter is used to keep track of which item is used next,
|
||||
and this is incremented on each iteration. When this counter has
|
||||
reached the length of the sequence the loop terminates. This means that
|
||||
if the suite deletes the current (or a previous) item from the
|
||||
sequence, the next item will be skipped (since it gets the index of
|
||||
the current item which has already been treated). Likewise, if the
|
||||
suite inserts an item in the sequence before the current item, the
|
||||
current item will be treated again the next time through the loop.
|
||||
This can lead to nasty bugs that can be avoided by making a temporary
|
||||
copy using a slice of the whole sequence, e.g.
|
||||
\index{loop!over mutable sequence}
|
||||
\index{mutable sequence!loop over}
|
||||
|
||||
\begin{verbatim}
|
||||
for x in a[:]:
|
||||
if x < 0: a.remove(x)
|
||||
\end{verbatim}
|
||||
|
||||
\section{The {\tt try} statement} \label{try}
|
||||
\stindex{try}
|
||||
|
||||
The \verb@try@ statement specifies exception handlers and/or cleanup
|
||||
code for a group of statements:
|
||||
|
||||
\begin{verbatim}
|
||||
try_stmt: try_exc_stmt | try_fin_stmt
|
||||
try_exc_stmt: "try" ":" suite
|
||||
("except" [condition ["," target]] ":" suite)+
|
||||
["else" ":" suite]
|
||||
try_fin_stmt: "try" ":" suite
|
||||
"finally" ":" suite
|
||||
\end{verbatim}
|
||||
|
||||
There are two forms of \verb@try@ statement: \verb@try...except@ and
|
||||
\verb@try...finally@. These forms cannot be mixed.
|
||||
|
||||
The \verb@try...except@ form specifies one or more exception handlers
|
||||
(the \verb@except@ clauses). When no exception occurs in the
|
||||
\verb@try@ clause, no exception handler is executed. When an
|
||||
exception occurs in the \verb@try@ suite, a search for an exception
|
||||
handler is started. This inspects the except clauses in turn until
|
||||
one is found that matches the exception. A condition-less except
|
||||
clause, if present, must be last; it matches any exception. For an
|
||||
except clause with a condition, that condition is evaluated, and the
|
||||
clause matches the exception if the resulting object is ``compatible''
|
||||
with the exception. An object is compatible with an exception if it
|
||||
is either the object that identifies the exception, or (for exceptions
|
||||
that are classes) it is a base class of the exception, or it is a
|
||||
tuple containing an item that is compatible with the exception. Note
|
||||
that the object identities must match, i.e. it must be the same
|
||||
object, not just an object with the same value.
|
||||
\kwindex{except}
|
||||
|
||||
If no except clause matches the exception, the search for an exception
|
||||
handler continues in the surrounding code and on the invocation stack.
|
||||
|
||||
If the evaluation of a condition in the header of an except clause
|
||||
raises an exception, the original search for a handler is cancelled
|
||||
and a search starts for the new exception in the surrounding code and
|
||||
on the call stack (it is treated as if the entire \verb@try@ statement
|
||||
raised the exception).
|
||||
|
||||
When a matching except clause is found, the exception's parameter is
|
||||
assigned to the target specified in that except clause, if present,
|
||||
and the except clause's suite is executed. When the end of this suite
|
||||
is reached, execution continues normally after the entire try
|
||||
statement. (This means that if two nested handlers exist for the same
|
||||
exception, and the exception occurs in the try clause of the inner
|
||||
handler, the outer handler will not handle the exception.)
|
||||
|
||||
Before an except clause's suite is executed, details about the
|
||||
exception are assigned to three variables in the \verb@sys@ module:
|
||||
\verb@sys.exc_type@ receives the object identifying the exception;
|
||||
\verb@sys.exc_value@ receives the exception's parameter;
|
||||
\verb@sys.exc_traceback@ receives a traceback object (see section
|
||||
\ref{traceback}) identifying the point in the program where the
|
||||
exception occurred.
|
||||
\bimodindex{sys}
|
||||
\ttindex{exc_type}
|
||||
\ttindex{exc_value}
|
||||
\ttindex{exc_traceback}
|
||||
\obindex{traceback}
|
||||
|
||||
The optional \verb@else@ clause is executed when no exception occurs
|
||||
in the \verb@try@ clause. Exceptions in the \verb@else@ clause are
|
||||
not handled by the preceding \verb@except@ clauses.
|
||||
\kwindex{else}
|
||||
|
||||
The \verb@try...finally@ form specifies a `cleanup' handler. The
|
||||
\verb@try@ clause is executed. When no exception occurs, the
|
||||
\verb@finally@ clause is executed. When an exception occurs in the
|
||||
\verb@try@ clause, the exception is temporarily saved, the
|
||||
\verb@finally@ clause is executed, and then the saved exception is
|
||||
re-raised. If the \verb@finally@ clause raises another exception or
|
||||
executes a \verb@return@, \verb@break@ or \verb@continue@ statement,
|
||||
the saved exception is lost.
|
||||
\kwindex{finally}
|
||||
|
||||
When a \verb@return@ or \verb@break@ statement is executed in the
|
||||
\verb@try@ suite of a \verb@try...finally@ statement, the
|
||||
\verb@finally@ clause is also executed `on the way out'. A
|
||||
\verb@continue@ statement is illegal in the \verb@try@ clause. (The
|
||||
reason is a problem with the current implementation --- this
|
||||
restriction may be lifted in the future).
|
||||
\stindex{return}
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
\section{Function definitions} \label{function}
|
||||
\indexii{function}{definition}
|
||||
|
||||
A function definition defines a user-defined function object (see
|
||||
section \ref{types}):\footnote{The new syntax to receive arbitrary
|
||||
keyword arguments is not yet documented in this manual. See chapter
|
||||
12 of the Tutorial.}
|
||||
\obindex{user-defined function}
|
||||
\obindex{function}
|
||||
|
||||
\begin{verbatim}
|
||||
funcdef: "def" funcname "(" [parameter_list] ")" ":" suite
|
||||
parameter_list: (defparameter ",")* ("*" identifier [, "**" identifier]
|
||||
| "**" identifier
|
||||
| defparameter [","])
|
||||
defparameter: parameter ["=" condition]
|
||||
sublist: parameter ("," parameter)* [","]
|
||||
parameter: identifier | "(" sublist ")"
|
||||
funcname: identifier
|
||||
\end{verbatim}
|
||||
|
||||
A function definition is an executable statement. Its execution binds
|
||||
the function name in the current local name space to a function object
|
||||
(a wrapper around the executable code for the function). This
|
||||
function object contains a reference to the current global name space
|
||||
as the global name space to be used when the function is called.
|
||||
\indexii{function}{name}
|
||||
\indexii{name}{binding}
|
||||
|
||||
The function definition does not execute the function body; this gets
|
||||
executed only when the function is called.
|
||||
|
||||
When one or more top-level parameters have the form {\em parameter =
|
||||
condition}, the function is said to have ``default parameter values''.
|
||||
Default parameter values are evaluated when the function definition is
|
||||
executed. For a parameter with a default value, the correponding
|
||||
argument may be omitted from a call, in which case the parameter's
|
||||
default value is substituted. If a parameter has a default value, all
|
||||
following parameters must also have a default value --- this is a
|
||||
syntactic restriction that is not expressed by the grammar.%
|
||||
\footnote{Currently this is not checked; instead,
|
||||
{\tt def f(a=1,b)} is interpreted as {\tt def f(a=1,b=None)}.}
|
||||
\indexiii{default}{parameter}{value}
|
||||
|
||||
Function call semantics are described in section \ref{calls}. When a
|
||||
user-defined function is called, first missing arguments for which a
|
||||
default value exists are supplied; then the arguments (a.k.a. actual
|
||||
parameters) are bound to the (formal) parameters, as follows:
|
||||
\indexii{function}{call}
|
||||
\indexiii{user-defined}{function}{call}
|
||||
\index{parameter}
|
||||
\index{argument}
|
||||
\indexii{parameter}{formal}
|
||||
\indexii{parameter}{actual}
|
||||
|
||||
\begin{itemize}
|
||||
|
||||
\item
|
||||
If there are no formal parameters, there must be no arguments.
|
||||
|
||||
\item
|
||||
If the formal parameter list does not end in a star followed by an
|
||||
identifier, there must be exactly as many arguments as there are
|
||||
parameters in the formal parameter list (at the top level); the
|
||||
arguments are assigned to the formal parameters one by one. Note that
|
||||
the presence or absence of a trailing comma at the top level in either
|
||||
the formal or the actual parameter list makes no difference. The
|
||||
assignment to a formal parameter is performed as if the parameter
|
||||
occurs on the left hand side of an assignment statement whose right
|
||||
hand side's value is that of the argument.
|
||||
|
||||
\item
|
||||
If the formal parameter list ends in a star followed by an identifier,
|
||||
preceded by zero or more comma-followed parameters, there must be at
|
||||
least as many arguments as there are parameters preceding the star.
|
||||
Call this number {\em N}. The first {\em N} arguments are assigned to
|
||||
the corresponding formal parameters in the way descibed above. A
|
||||
tuple containing the remaining arguments, if any, is then assigned to
|
||||
the identifier following the star. This variable will always be a
|
||||
tuple: if there are no extra arguments, its value is \verb@()@, if
|
||||
there is just one extra argument, it is a singleton tuple.
|
||||
\indexii{variable length}{parameter list}
|
||||
|
||||
\end{itemize}
|
||||
|
||||
Note that the `variable length parameter list' feature only works at
|
||||
the top level of the parameter list; individual parameters use a model
|
||||
corresponding more closely to that of ordinary assignment. While the
|
||||
latter model is generally preferable, because of the greater type
|
||||
safety it offers (wrong-sized tuples aren't silently mistreated),
|
||||
variable length parameter lists are a sufficiently accepted practice
|
||||
in most programming languages that a compromise has been worked out.
|
||||
(And anyway, assignment has no equivalent for empty argument lists.)
|
||||
|
||||
It is also possible to create anonymous functions (functions not bound
|
||||
to a name), for immediate use in expressions. This uses lambda forms,
|
||||
described in section \ref{lambda}.
|
||||
\indexii{lambda}{form}
|
||||
|
||||
\section{Class definitions} \label{class}
|
||||
\indexii{class}{definition}
|
||||
|
||||
A class definition defines a class object (see section \ref{types}):
|
||||
\obindex{class}
|
||||
|
||||
\begin{verbatim}
|
||||
classdef: "class" classname [inheritance] ":" suite
|
||||
inheritance: "(" [condition_list] ")"
|
||||
classname: identifier
|
||||
\end{verbatim}
|
||||
|
||||
A class definition is an executable statement. It first evaluates the
|
||||
inheritance list, if present. Each item in the inheritance list
|
||||
should evaluate to a class object. The class's suite is then executed
|
||||
in a new execution frame (see section \ref{execframes}), using a newly
|
||||
created local name space and the original global name space.
|
||||
(Usually, the suite contains only function definitions.) When the
|
||||
class's suite finishes execution, its execution frame is discarded but
|
||||
its local name space is saved. A class object is then created using
|
||||
the inheritance list for the base classes and the saved local name
|
||||
space for the attribute dictionary. The class name is bound to this
|
||||
class object in the original local name space.
|
||||
\index{inheritance}
|
||||
\indexii{class}{name}
|
||||
\indexii{name}{binding}
|
||||
\indexii{execution}{frame}
|
||||
105
Doc/ref8.tex
105
Doc/ref8.tex
@@ -1,105 +0,0 @@
|
||||
\chapter{Top-level components}
|
||||
|
||||
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,
|
||||
typed in interactively, from a module source file, etc. This chapter
|
||||
gives the syntax used in these cases.
|
||||
\index{interpreter}
|
||||
|
||||
\section{Complete Python programs}
|
||||
\index{program}
|
||||
|
||||
While a language specification need not prescribe how the language
|
||||
interpreter is invoked, it is useful to have a notion of a complete
|
||||
Python program. A complete Python program is executed in a minimally
|
||||
initialized environment: all built-in and standard modules are
|
||||
available, but none have been initialized, except for \verb@sys@
|
||||
(various system services), \verb@__builtin__@ (built-in functions,
|
||||
exceptions and \verb@None@) and \verb@__main__@. The latter is used
|
||||
to provide the local and global name space for execution of the
|
||||
complete program.
|
||||
\bimodindex{sys}
|
||||
\bimodindex{__main__}
|
||||
\bimodindex{__builtin__}
|
||||
|
||||
The syntax for a complete Python program is that for file input,
|
||||
described in the next section.
|
||||
|
||||
The interpreter may also be invoked in interactive mode; in this case,
|
||||
it does not read and execute a complete program but reads and executes
|
||||
one statement (possibly compound) at a time. The initial environment
|
||||
is identical to that of a complete program; each statement is executed
|
||||
in the name space of \verb@__main__@.
|
||||
\index{interactive mode}
|
||||
\bimodindex{__main__}
|
||||
|
||||
Under {\UNIX}, a complete program can be passed to the interpreter in
|
||||
three forms: with the {\bf -c} {\it string} command line option, as a
|
||||
file passed as the first command line argument, or as standard input.
|
||||
If the file or standard input is a tty device, the interpreter enters
|
||||
interactive mode; otherwise, it executes the file as a complete
|
||||
program.
|
||||
\index{UNIX}
|
||||
\index{command line}
|
||||
\index{standard input}
|
||||
|
||||
\section{File input}
|
||||
|
||||
All input read from non-interactive files has the same form:
|
||||
|
||||
\begin{verbatim}
|
||||
file_input: (NEWLINE | statement)*
|
||||
\end{verbatim}
|
||||
|
||||
This syntax is used in the following situations:
|
||||
|
||||
\begin{itemize}
|
||||
|
||||
\item when parsing a complete Python program (from a file or from a string);
|
||||
|
||||
\item when parsing a module;
|
||||
|
||||
\item when parsing a string passed to the \verb@exec@ statement;
|
||||
|
||||
\end{itemize}
|
||||
|
||||
\section{Interactive input}
|
||||
|
||||
Input in interactive mode is parsed using the following grammar:
|
||||
|
||||
\begin{verbatim}
|
||||
interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE
|
||||
\end{verbatim}
|
||||
|
||||
Note that a (top-level) compound statement must be followed by a blank
|
||||
line in interactive mode; this is needed to help the parser detect the
|
||||
end of the input.
|
||||
|
||||
\section{Expression input}
|
||||
\index{input}
|
||||
|
||||
There are two forms of expression input. Both ignore leading
|
||||
whitespace.
|
||||
|
||||
The string argument to \verb@eval()@ must have the following form:
|
||||
\bifuncindex{eval}
|
||||
|
||||
\begin{verbatim}
|
||||
eval_input: condition_list NEWLINE*
|
||||
\end{verbatim}
|
||||
|
||||
The input line read by \verb@input()@ must have the following form:
|
||||
\bifuncindex{input}
|
||||
|
||||
\begin{verbatim}
|
||||
input_input: condition_list NEWLINE
|
||||
\end{verbatim}
|
||||
|
||||
Note: to read `raw' input line without interpretation, you can use the
|
||||
built-in function \verb@raw_input()@ or the \verb@readline()@ method
|
||||
of file objects.
|
||||
\obindex{file}
|
||||
\index{input!raw}
|
||||
\index{raw input}
|
||||
\bifuncindex{raw_index}
|
||||
\ttindex{readline}
|
||||
Reference in New Issue
Block a user