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