8.1 完整的Python程序 Complete Python programs

While a language specification need not prescribe how the language interpreter is invoked, it is useful to have a notion of a complete Python program. A complete Python program is executed in a minimally initialized environment: all built-in and standard modules are available, but none have been initialized, except for sys (various system services), __builtin__ (built-in functions, exceptions and None) and __main__. The latter is used to provide the local and global namespace for execution of the complete program.

尽管语法规格说明不需要指明语言的解释器是如何执行的,对一个完整的Python程序的了解也是有用的。一个完整的Python程序运行在一个最低限度的初始环境中:所有内置和标准模块都是可用的,但都没有被初始化,除了sys(各种系统服务)模块、__builtin__(内置函数、异常和None)模块和__main__模块。__main__被用来为完整程序的运行提供局部和全局名字空间。

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

针对于文件输入来说的完整的Python程序语法,在下一节给出描述。

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

解释器也可以以交互方式运行;在这种情况下,它并不读取和运行一个完整程序,而是一次读取和运行一条语句(可能是复合语句)。这种初始环境与完整程序环境是相同的;每条语句都是在__main__名字空间下运行。

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

在Unix上,一个完整程序可以以三种形式传给解释器:使用-c字符串命令行选项,以一个文件作为命令行的第一个参数,或作为标准输入。如果文件或标准输入是一个tty(终端)设备,解释器进行交互模式;否则,它把文件作为一个完整程序来运行。