source: vendor/python/2.5/Doc/ref/ref8.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.6 KB
Line 
1\chapter{Top-level components\label{top-level}}
2
3The Python interpreter can get its input from a number of sources:
4from a script passed to it as standard input or as program argument,
5typed in interactively, from a module source file, etc. This chapter
6gives the syntax used in these cases.
7\index{interpreter}
8
9
10\section{Complete Python programs\label{programs}}
11\index{program}
12
13While a language specification need not prescribe how the language
14interpreter is invoked, it is useful to have a notion of a complete
15Python program. A complete Python program is executed in a minimally
16initialized environment: all built-in and standard modules are
17available, but none have been initialized, except for \module{sys}
18(various system services), \module{__builtin__} (built-in functions,
19exceptions and \code{None}) and \module{__main__}. The latter is used
20to provide the local and global namespace for execution of the
21complete program.
22\refbimodindex{sys}
23\refbimodindex{__main__}
24\refbimodindex{__builtin__}
25
26The syntax for a complete Python program is that for file input,
27described in the next section.
28
29The interpreter may also be invoked in interactive mode; in this case,
30it does not read and execute a complete program but reads and executes
31one statement (possibly compound) at a time. The initial environment
32is identical to that of a complete program; each statement is executed
33in the namespace of \module{__main__}.
34\index{interactive mode}
35\refbimodindex{__main__}
36
37Under \UNIX, a complete program can be passed to the interpreter in
38three forms: with the \programopt{-c} \var{string} command line option, as a
39file passed as the first command line argument, or as standard input.
40If the file or standard input is a tty device, the interpreter enters
41interactive mode; otherwise, it executes the file as a complete
42program.
43\index{UNIX}
44\index{command line}
45\index{standard input}
46
47
48\section{File input\label{file-input}}
49
50All input read from non-interactive files has the same form:
51
52\begin{productionlist}
53 \production{file_input}
54 {(NEWLINE | \token{statement})*}
55\end{productionlist}
56
57This syntax is used in the following situations:
58
59\begin{itemize}
60
61\item when parsing a complete Python program (from a file or from a string);
62
63\item when parsing a module;
64
65\item when parsing a string passed to the \keyword{exec} statement;
66
67\end{itemize}
68
69
70\section{Interactive input\label{interactive}}
71
72Input in interactive mode is parsed using the following grammar:
73
74\begin{productionlist}
75 \production{interactive_input}
76 {[\token{stmt_list}] NEWLINE | \token{compound_stmt} NEWLINE}
77\end{productionlist}
78
79Note that a (top-level) compound statement must be followed by a blank
80line in interactive mode; this is needed to help the parser detect the
81end of the input.
82
83
84\section{Expression input\label{expression-input}}
85\index{input}
86
87There are two forms of expression input. Both ignore leading
88whitespace.
89The string argument to \function{eval()} must have the following form:
90\bifuncindex{eval}
91
92\begin{productionlist}
93 \production{eval_input}
94 {\token{expression_list} NEWLINE*}
95\end{productionlist}
96
97The input line read by \function{input()} must have the following form:
98\bifuncindex{input}
99
100\begin{productionlist}
101 \production{input_input}
102 {\token{expression_list} NEWLINE}
103\end{productionlist}
104
105Note: to read `raw' input line without interpretation, you can use the
106built-in function \function{raw_input()} or the \method{readline()} method
107of file objects.
108\obindex{file}
109\index{input!raw}
110\index{raw input}
111\bifuncindex{raw_input}
112\withsubitem{(file method)}{\ttindex{readline()}}
Note: See TracBrowser for help on using the repository browser.