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