blob: 485b34dbc738df90fbc45372966f54ee9d86524e [file] [log] [blame]
Fred Drake61c77281998-07-28 19:34:22 +00001\chapter{Top-level components\label{top-level}}
Fred Drakef6669171998-05-06 19:52:49 +00002
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
Fred Drake61c77281998-07-28 19:34:22 +00009\section{Complete Python programs\label{programs}}
Fred Drakef6669171998-05-06 19:52:49 +000010\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
Fred Drake5c07d9b1998-05-14 19:37:06 +000016available, but none have been initialized, except for \module{sys}
17(various system services), \module{__builtin__} (built-in functions,
18exceptions and \code{None}) and \module{__main__}. The latter is used
Guido van Rossumd5405091998-07-24 18:56:17 +000019to provide the local and global namespace for execution of the
Fred Drakef6669171998-05-06 19:52:49 +000020complete program.
21\refbimodindex{sys}
22\refbimodindex{__main__}
23\refbimodindex{__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
Guido van Rossumd5405091998-07-24 18:56:17 +000031is identical to that of a coplete program; each statement is executed
32in the namespace of \module{__main__}.
Fred Drakef6669171998-05-06 19:52:49 +000033\index{interactive mode}
34\refbimodindex{__main__}
35
36Under {\UNIX}, a complete program can be passed to the interpreter in
Fred Drakea24f7b71999-11-09 17:03:45 +000037three forms: with the \programopt{-c} \var{string} command line option, as a
Fred Drakef6669171998-05-06 19:52:49 +000038file 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
Fred Drake61c77281998-07-28 19:34:22 +000046\section{File input\label{file-input}}
Fred Drakef6669171998-05-06 19:52:49 +000047
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
Fred Drake5c07d9b1998-05-14 19:37:06 +000062\item when parsing a string passed to the \keyword{exec} statement;
Fred Drakef6669171998-05-06 19:52:49 +000063
64\end{itemize}
65
Fred Drake61c77281998-07-28 19:34:22 +000066\section{Interactive input\label{interactive}}
Fred Drakef6669171998-05-06 19:52:49 +000067
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
Fred Drake61c77281998-07-28 19:34:22 +000078\section{Expression input\label{expression-input}}
Fred Drakef6669171998-05-06 19:52:49 +000079\index{input}
80
81There are two forms of expression input. Both ignore leading
82whitespace.
Fred Drake5c07d9b1998-05-14 19:37:06 +000083The string argument to \function{eval()} must have the following form:
Fred Drakef6669171998-05-06 19:52:49 +000084\bifuncindex{eval}
85
86\begin{verbatim}
Guido van Rossumd5405091998-07-24 18:56:17 +000087eval_input: expression_list NEWLINE*
Fred Drakef6669171998-05-06 19:52:49 +000088\end{verbatim}
89
Fred Drake5c07d9b1998-05-14 19:37:06 +000090The input line read by \function{input()} must have the following form:
Fred Drakef6669171998-05-06 19:52:49 +000091\bifuncindex{input}
92
93\begin{verbatim}
Guido van Rossumd5405091998-07-24 18:56:17 +000094input_input: expression_list NEWLINE
Fred Drakef6669171998-05-06 19:52:49 +000095\end{verbatim}
96
97Note: to read `raw' input line without interpretation, you can use the
Fred Drake5c07d9b1998-05-14 19:37:06 +000098built-in function \function{raw_input()} or the \method{readline()} method
Fred Drakef6669171998-05-06 19:52:49 +000099of file objects.
100\obindex{file}
101\index{input!raw}
102\index{raw input}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000103\bifuncindex{raw_input}
104\withsubitem{(file method)}{\ttindex{readline()}}