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