blob: d10c87f5bea880f3ab5fc2cb37c7119a1abc8323 [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 Drake2829f1c2001-06-23 05:27:20 +00009
Fred Drake61c77281998-07-28 19:34:22 +000010\section{Complete Python programs\label{programs}}
Fred Drakef6669171998-05-06 19:52:49 +000011\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
Fred Drake5c07d9b1998-05-14 19:37:06 +000017available, 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
Guido van Rossumd5405091998-07-24 18:56:17 +000020to provide the local and global namespace for execution of the
Fred Drakef6669171998-05-06 19:52:49 +000021complete 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
Fred Drake2f4453a2001-01-26 15:27:35 +000032is identical to that of a complete program; each statement is executed
Guido van Rossumd5405091998-07-24 18:56:17 +000033in the namespace of \module{__main__}.
Fred Drakef6669171998-05-06 19:52:49 +000034\index{interactive mode}
35\refbimodindex{__main__}
36
37Under {\UNIX}, a complete program can be passed to the interpreter in
Fred Drakea24f7b71999-11-09 17:03:45 +000038three forms: with the \programopt{-c} \var{string} command line option, as a
Fred Drakef6669171998-05-06 19:52:49 +000039file 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
Fred Drake2829f1c2001-06-23 05:27:20 +000047
Fred Drake61c77281998-07-28 19:34:22 +000048\section{File input\label{file-input}}
Fred Drakef6669171998-05-06 19:52:49 +000049
50All input read from non-interactive files has the same form:
51
Fred Drakecb4638a2001-07-06 22:49:53 +000052\begin{productionlist}
53 \production{file_input}
54 {(NEWLINE | \token{statement})*}
55\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000056
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
Fred Drake5c07d9b1998-05-14 19:37:06 +000065\item when parsing a string passed to the \keyword{exec} statement;
Fred Drakef6669171998-05-06 19:52:49 +000066
67\end{itemize}
68
Fred Drake2829f1c2001-06-23 05:27:20 +000069
Fred Drake61c77281998-07-28 19:34:22 +000070\section{Interactive input\label{interactive}}
Fred Drakef6669171998-05-06 19:52:49 +000071
72Input in interactive mode is parsed using the following grammar:
73
Fred Drakecb4638a2001-07-06 22:49:53 +000074\begin{productionlist}
75 \production{interactive_input}
76 {[\token{stmt_list}] NEWLINE | \token{compound_stmt} NEWLINE}
77\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000078
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
Fred Drake2829f1c2001-06-23 05:27:20 +000083
Fred Drake61c77281998-07-28 19:34:22 +000084\section{Expression input\label{expression-input}}
Fred Drakef6669171998-05-06 19:52:49 +000085\index{input}
86
87There are two forms of expression input. Both ignore leading
88whitespace.
Fred Drake5c07d9b1998-05-14 19:37:06 +000089The string argument to \function{eval()} must have the following form:
Fred Drakef6669171998-05-06 19:52:49 +000090\bifuncindex{eval}
91
Fred Drakecb4638a2001-07-06 22:49:53 +000092\begin{productionlist}
93 \production{eval_input}
94 {\token{expression_list} NEWLINE*}
95\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000096
Fred Drake5c07d9b1998-05-14 19:37:06 +000097The input line read by \function{input()} must have the following form:
Fred Drakef6669171998-05-06 19:52:49 +000098\bifuncindex{input}
99
Fred Drakecb4638a2001-07-06 22:49:53 +0000100\begin{productionlist}
101 \production{input_input}
102 {\token{expression_list} NEWLINE}
103\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000104
105Note: to read `raw' input line without interpretation, you can use the
Fred Drake5c07d9b1998-05-14 19:37:06 +0000106built-in function \function{raw_input()} or the \method{readline()} method
Fred Drakef6669171998-05-06 19:52:49 +0000107of file objects.
108\obindex{file}
109\index{input!raw}
110\index{raw input}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000111\bifuncindex{raw_input}
112\withsubitem{(file method)}{\ttindex{readline()}}