Movatterモバイル変換


[0]ホーム

URL:


Navigation

9. Top-level components

The Python interpreter can get its input from a number of sources: from a scriptpassed to it as standard input or as program argument, typed in interactively,from a module source file, etc. This chapter gives the syntax used in thesecases.

9.1. Complete Python programs

While a language specification need not prescribe how the language interpreteris invoked, it is useful to have a notion of a complete Python program. Acomplete Python program is executed in a minimally initialized environment: allbuilt-in and standard modules are available, but none have been initialized,except forsys (various system services),builtins (built-infunctions, exceptions andNone) and__main__. The latter is used toprovide the local and global namespace for execution of the complete program.

The syntax for a complete Python program is that for file input, described inthe next section.

The interpreter may also be invoked in interactive mode; in this case, it doesnot read and execute a complete program but reads and executes one statement(possibly compound) at a time. The initial environment is identical to that ofa complete program; each statement is executed in the namespace of__main__.

Under Unix, a complete program can be passed to the interpreter in three forms:with the-cstring command line option, as a file passed as thefirst command line argument, or as standard input. If the file or standardinput is a tty device, the interpreter enters interactive mode; otherwise, itexecutes the file as a complete program.

9.2. File input

All input read from non-interactive files has the same form:

file_input ::=  (NEWLINE |statement)*

This syntax is used in the following situations:

  • when parsing a complete Python program (from a file or from a string);
  • when parsing a module;
  • when parsing a string passed to theexec() function;

9.3. Interactive input

Input in interactive mode is parsed using the following grammar:

interactive_input ::=  [stmt_list] NEWLINE |compound_stmt NEWLINE

Note that a (top-level) compound statement must be followed by a blank line ininteractive mode; this is needed to help the parser detect the end of the input.

9.4. Expression input

There are two forms of expression input. Both ignore leading whitespace. Thestring argument toeval() must have the following form:

eval_input ::=expression_list NEWLINE*

Note: to read ‘raw’ input line without interpretation, you can use thereadline() method of file objects, includingsys.stdin.

Table Of Contents

Previous topic

8. Compound statements

Next topic

10. Full Grammar specification

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp