blob: a9e688a8405dad153e2dea9cb8a42932861a651d [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
Guido van Rossume9914961994-08-01 12:38:14 +000016available, 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
Guido van Rossumda43a4a1992-08-14 09:17:29 +000019to provide the local and global name space for execution of the
20complete program.
21\bimodindex{sys}
22\bimodindex{__main__}
Guido van Rossum4bd023f1993-10-27 13:49:20 +000023\bimodindex{__builtin__}
Guido van Rossumda43a4a1992-08-14 09:17:29 +000024
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
Guido van Rossume9914961994-08-01 12:38:14 +000032in the name space of \verb@__main__@.
Guido van Rossumda43a4a1992-08-14 09:17:29 +000033\index{interactive mode}
Guido van Rossumaaec4031995-03-21 14:41:57 +000034\bimodindex{__main__}
Guido van Rossumda43a4a1992-08-14 09:17:29 +000035
36Under {\UNIX}, a complete program can be passed to the interpreter in
37three forms: with the {\bf -c} {\it string} command line option, as a
38file passed as the first command line argument, or as standard input.
39If the file or standard input is a tty device, the interpreter enters
40interactive mode; otherwise, it executes the file as a complete
41program.
42\index{UNIX}
43\index{command line}
44\index{standard input}
45
46\section{File input}
47
48All input read from non-interactive files has the same form:
49
50\begin{verbatim}
51file_input: (NEWLINE | statement)*
52\end{verbatim}
53
54This syntax is used in the following situations:
55
56\begin{itemize}
57
58\item when parsing a complete Python program (from a file or from a string);
59
60\item when parsing a module;
61
Guido van Rossume9914961994-08-01 12:38:14 +000062\item when parsing a string passed to the \verb@exec@ statement;
Guido van Rossumda43a4a1992-08-14 09:17:29 +000063
64\end{itemize}
65
66\section{Interactive input}
67
68Input in interactive mode is parsed using the following grammar:
69
70\begin{verbatim}
71interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE
72\end{verbatim}
73
74Note that a (top-level) compound statement must be followed by a blank
75line in interactive mode; this is needed to help the parser detect the
76end of the input.
77
78\section{Expression input}
79\index{input}
80
81There are two forms of expression input. Both ignore leading
82whitespace.
83
Guido van Rossume9914961994-08-01 12:38:14 +000084The string argument to \verb@eval()@ must have the following form:
Guido van Rossumda43a4a1992-08-14 09:17:29 +000085\bifuncindex{eval}
86
87\begin{verbatim}
88eval_input: condition_list NEWLINE*
89\end{verbatim}
90
Guido van Rossume9914961994-08-01 12:38:14 +000091The input line read by \verb@input()@ must have the following form:
Guido van Rossumda43a4a1992-08-14 09:17:29 +000092\bifuncindex{input}
93
94\begin{verbatim}
95input_input: condition_list NEWLINE
96\end{verbatim}
97
98Note: to read `raw' input line without interpretation, you can use the
Guido van Rossume9914961994-08-01 12:38:14 +000099built-in function \verb@raw_input()@ or the \verb@readline()@ method
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000100of file objects.
101\obindex{file}
102\index{input!raw}
103\index{raw input}
104\bifuncindex{raw_index}
105\ttindex{readline}