似于调用 :func:`input` 和 :func:`raw_input` 这样的输入请求,来自于确定
的 *文件* 。因为在解析器开始执行之前,文件已经完全读入,所以程序指向文件尾。在前一种情况
(这通常是你需要的) 它们读取联接到 Python 解释器的标准输入,无关它们是文件还是其它设备。
When a script file is used, it is sometimes useful to be able to run the script
and enter interactive mode afterwards. This can be done by passing :option:`-i`
before the script. (This does not work if the script is read from standard
input, for the same reason as explained in the previous paragraph.)
使用脚本文件时,经常会运行脚本然后进入交互模式。这也可以通过在脚本之前
加上 :option:`-i` 参数来实现。(如果脚本来自标准输入,就不能这样运行,与前一段提到的原因一样。)
.. _tut-argpassing:
Argument Passing 参数传递
------------------------
When known to the interpreter, the script name and additional arguments
thereafter are passed to the script in the variable ``sys.argv``, which is a
list of strings. Its length is at least one; when no script and no arguments
are given, ``sys.argv[0]`` is an empty string. When the script name is given as
``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When
:option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When
:option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the
located module. Options found after :option:`-c` *command* or :option:`-m`
*module* are not consumed by the Python interpreter's option processing but
left in ``sys.argv`` for the command or module to handle.
调用解释器时,脚本名和附加参数传入一个名为 ``sys.argv`` 的字符串列表。没有
给定脚本和参数时,它至少也有一个元素: ``sys.argv[0]`` 此时为空字符串。脚本
名指定为 ``'-'`` (表示标准输入)时, ``sys.argv[0]`` 被设定为 ``'-'``
,使用 :option:`-c` 指令时, ``sys.argv[0]`` 被设定为 ``'-c'`` 。 使用
:option:`-m` *module* 参数时, ``sys.agv[0]`` 被设定
为指定模块的全名。:option:`-c` *command* 或者 :option:`-m` *module* 之
后的参数不会被 Python 解释器的选项处理机制所截获,而是留在
``sys.argv`` 中,供脚本命令操作。
.. _tut-interactive:
Interactive Mode 交互模式
------------------------
When commands are read from a tty, the interpreter is said to be in *interactive
mode*. In this mode it prompts for the next command with the *primary prompt*,
usually three greater-than signs (``>>>``); for continuation lines it prompts
with the *secondary prompt*, by default three dots (``...``). The interpreter
prints a welcome message stating its version number and a copyright notice
before printing the first prompt::
从 tty 读取命令时,我们称解释器工作于 *交互模式* 。这种模式下它根据
*主提示符*
来执行,主提示符通常标识为三个大于号( ``>>>`` );继续的部分被称为
*从属提示符* ,由三个点标识( ``... `` )。在第一行之前,解释器打印欢迎信息、
版本号和授权提示 ::
python
Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information.
>>>
Continuation lines are needed when entering a multi-line construct. As an
example, take a look at this :keyword:`if` statement::
输入多行结构时需要从属提示符了,例如,下面这个 if 语句 ::
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
... print "Be careful not to fall off!"
...
Be careful not to fall off!
.. _tut-interp:
The Interpreter and Its Environment 解释器及其环境
=================================================
.. _tut-error:
Error Handling 错误处理
----------------------
When an error occurs, the interpreter prints an error message and a stack trace.
In interactive mode, it then returns to the primary prompt; when input came from
a file, it exits with a nonzero exit status after printing the stack trace.
(Exceptions handled by an :keyword:`except` clause in a :keyword:`try` statement
are not errors in this context.) Some errors are unconditionally fatal and
cause an exit with a nonzero exit; this applies to internal inconsistencies and
some cases of running out of memory. All error messages are written to the
standard error stream; normal output from executed commands is written to
standard output.
有错误发生时,解释器打印一个错误信息和栈跟踪器。交互模式下,它返回主提
示符,如果从文件输入执行,它在打印栈跟踪器后以非零状态退出。(异常可以
由 :keyword:`try` 语句中的 :keyword:`except` 子句来控制,这样就不会出现
上文中的错误信息)有一些非常致命的错误会导致非零状态下退出,这由通常由
内部矛盾和内存溢出造成。所有的错误信息都写入标准错误流;命令中执行的普
通输出写入标准输出。
Typing the interrupt character (usually Control-C or DEL) to the primary or
secondary prompt cancels the input and returns to the primary prompt. [#]_
Typing an interrupt while a command is executing raises the
:exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try`
statement.
在主提示符或附属提示符输入中断符(通常是Control-C 或者 DEL)就会取消当
前输入,回到主命令行。 2.2.执行命令时输入一个中断符会抛出一个
:exc:`KeyboardInterrupt` 异常,它可以被 :keyword:`try` 句截获。
.. _tut-scripts:
Executable Python Scripts 执行 Python 脚本
-----------------------------------------
On BSD'ish Unix systems, Python scripts can be made directly executable, like
shell scripts, by putting the line ::
BSD类的 Unix系统中,Python 脚本可以像 Shell 脚本那样直接执行。只要在脚
本文件开头写一行命令,指定文件和模式 ::
#! /usr/bin/env python
(assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning
of the script and giving the file an executable mode. The ``#!`` must be the
first two characters of the file. On some platforms, this first line must end
with a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line
ending. Note that the hash, or pound, character, ``'#'``, is used to start a
comment in Python.
(要确认 Python 解释器在用户的 :envvar:`PATH` 中) ``#!`` 必须是文件的前
两个字符,在某些平台上,第一行必须以 Unix 风格的行结束符( ``'\n'`` )结束,
不能用 Windows ( ``'\r\n'`` ) 的结束符。注意, ``'#'`` 是Python中是行
注释的起始符。
The script can be given an executable mode, or permission, using the
:program:`chmod` command::
脚本可以通过 chmod 命令指定执行模式和权限 ::
$ chmod +x myscript.py
On Windows systems, there is no notion of an "executable mode". The Python
installer automatically associates ``.py`` files with ``python.exe`` so that
a double-click on a Python file will run it as a script. The extension can
also be ``.pyw``, in that case, the console window that normally appears is
suppressed.
Windows 系统上没有“执行模式”。 Python 安装程序自动将 ``.py`` 文件关
联到 ``python.exe`` ,所以在 Python 文件图标上双击,它就会作为脚本执行。
同样 ``.pyw`` 也作了这样的关联,通常它执行时不会显示控制台窗口。
Source Code Encoding 源程序编码
------------------------------
It is possible to use encodings different than ASCII in Python source files. The
best way to do it is to put one more special comment line right after the ``#!``
line to define the source file encoding::
Python 的源文件可以通过编码使用 ASCII 以外的字符集。最好的做法是在 #!
行后面用一个特殊的注释行来定义字符集 ::
# -*- coding: encoding -*-
With that declaration, all characters in the source file will be treated as
having the encoding *encoding*, and it will be possible to directly write
Unicode string literals in the selected encoding. The list of possible
encodings can be found in the Python Library Reference, in the section on
:mod:`codecs`.
根据这个声明,Python 会尝试将文件中的字符编码转为 *encoding* 编码。并且,它
尽可能的将指定的编码直接写成 Unicode 文本。在 Python 库参考手册 中
:mod:`codecs` 部份可以找到可用的编码列表(推荐使用 utf-8 处理中文--译者注)。
For example, to write Unicode literals including the Euro currency symbol, the
ISO-8859-15 encoding can be used, with the Euro symbol having the ordinal value
164. This script will print the value 8364 (the Unicode codepoint corresponding
to the Euro symbol) and then exit::
例如,可以用 ISO-8859-15 编码可以用来编写包含欧元符号的 Unicode 文本,
其编码值为 164。这个脚本会输出 8364 (欧元符号的 Unicode 对应编码) 然后退出 ::
# -*- coding: iso-8859-15 -*-
currency = u"
阅读(950) | 评论(0) | 转发(0) |