每天写1000字007不写就出局

2. Using the Python Interpreter

2019-08-14  本文已影响5人  赵敏是程序媛

2.1. Invoking the Interpreter

The Python interpreter is usually installed as /usr/local/bin/python3.7 on those machines where it is available; putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:

python3.7

to the shell. 1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative location.)

通常在可用的机器上Python解释器安装在/usr/local/bin/python3.7目录,将/usr/local/bin放在你的unix shell查找路径,这使得使用python3.7命令成为可能。因为解释器安装目录可以通过安装选项指定目录,所以也可以安装到别的路径。请与你的本地的python专家或者系统专家一起排查。(比如,/usr/local/python目录是一个可选择的本地路径。)

On Windows machines where you have installed from the Microsoft Store, the python3.7 command will be available. If you have the py.exe launcher installed, you can use the py command. See Excursus: Setting environment variables for other ways to launch Python.

在Windows机器上,你可以通过从微软商店安装,这样python3.7命令就可以使用。如果你已经安装了py.exe launcher,你就可以使用py命令。对于其他安装python的方式请参考附件:设置环境变量。

Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command: quit().

在主提示符处键入文件结束符(Unix上是Control-D, Windows上是Control-Z)会导致解释器以0退出状态退出。如果退出不生效,你可以使用quit命令退出解释器。

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline. Perhaps the quickest check to see whether command line editing is supported is typing Control-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive Input Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed, command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.

在支持行阅读的系统上,解释器的行编辑特性包含交互式编辑,历史替换和编码完成。要查看是否支持命令行编辑,最快的检查方法可能是在得到的第一个Python提示符中键入Control-P。如果出现哔哔声,说明你已经支持了命令行编辑;对于关键字介绍请查看附件交互式输入编辑和历史替换。如果什么都没发生,或者输出了^P,说明不支持命令行编辑;你只能使用退格从当前行中删除字符。(译者的说,一般在unix shell界面输入python,输出命令行交互界面,可以执行行编辑,它支持即时的读和执行操作)

The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.

解释器的操作有点像Unix shell:当调用标准输出链接到tty设备的时候,它交互式读取并且执行命令;当调用文件名参数或者将一个文件作为标准输入时,它从指定的文件读取并且执行脚本。(译者说:比如python test.py,对于shell就是sh test.sh,当敲下上面两个命令之一时,就会执行test.py或者test.sh脚本)

A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in command, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety with single quotes.

第二种启动解释器的方式是在命令行执行python -c command [arg]...命令,这类似shell的-c选项。因为python语句通常包含空格或者其他针对shell的字符,通常建议用单引号完整地引用命令。(译者说,类似python -c 'print(333)'命令执行后显示333,效果类似在python交互式环境下执行单行命令)

Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line.

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 -i before the script.

部分python模块也可以作为脚本使用。可以通过python -m module [arg]...引用模块,只要在命令行中拼出模块的全名来执行模块源码文件。

当使用脚本文件时,既可以运行脚本,也可以进入交互式模型。在指定的脚本之前传入-i参数就可以。

All command line options are described in Command line and environment.

所有命令行选择都在命令行和环境中做了描述。

2.1.1. Argument Passing

When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module. You can access this list by executing import sys. The length of the list 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 -c command is used, sys.argv[0] is set to '-c'. When -m module is used, sys.argv[0] is set to the full name of the located module. Options found after -c command or -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变量。你可以通过执行import sys来访问该列表。该列表的长度至少为1;当没有给定脚本和参数时,sys.argv[0]是空串。当给定的脚本名称是‘-’时(即标准输入)sys.argv[0]设置为‘-’。当使用-c命令行时,sys.argv[0]设置为‘-c’。当使用-m模块时,sys.argv[0]设置为本地模块的全名。在-c命令或-m模块之后找到的选项不会被Python解释器的选项处理所使用,而是留在命令或模块要处理的sys.argv中。

2.1.2. 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:

$ python3.7Python 3.7 (default, Sep 16 2015, 09:25:04)[GCC 4.8.2] on linuxType "help", "copyright", "credits" or "license" for more information.>>>

当从tty中读取命令行时,解释器就是交互式模式。在这种模式中,使用>>>提示输入下一条命令;对于连续的行使用...作为提示。解释器在开始打印第一个提示语之前会打印欢迎信息,包含启动的版本号和权限注意事项。(译者说,比如python3.7就是版本号,on linuxType "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 ifstatement:

>>>

>>> the_world_is_flat = True>>> if the_world_is_flat:...     print("Be careful not to fall off!")...Be careful not to fall off!

For more on interactive mode, see Interactive Mode.

连续行需要输入多行结构。比如,下面的if语句:

>>> the_world_is_flat = True

>>> if the_world_is_flat:

...     print("Be careful not to fall off!")

...Be careful not to fall off!

(译者说,下面的截图是译者自己做的测试)

2.2. The Interpreter and Its Environment

2.2.1. Source Code Encoding

By default, Python source files are treated as encoded in UTF-8. In that encoding, characters of most languages in the world can be used simultaneously in string literals, identifiers and comments — although the standard library only uses ASCII characters for identifiers, a convention that any portable code should follow. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file.

默认,python源码文件以UTF-8的方式编码。在这种编码中,世界上大部分语言的字符可以在文字、标示符和注释中同时使用——对于标示符尽管标准库只使用ASCII字符,但是任何端口编码都需要遵守该约定。要正确显示所有这些字符,编辑器必须识别文件是UTF-8,并且必须使用支持文件中所有字符的字体。

To declare an encoding other than the default one, a special comment line should be added as the first line of the file. The syntax is as follows:

# -*- coding: encoding -*-

where encoding is one of the valid codecs supported by Python.

为了申明非默认的编码方式,需要在文件第一行添加特殊的注释行。语法如下:

# -*- coding: encoding -*-

这里的编码方式在python codecs中有效。

For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:

# -*- coding: cp1252 -*-

比如,为了什么windows-1253编码方式,源码的第一行需要是下面一行:

# -*- coding: cp1252 -*-

One exception to the first line rule is when the source code starts with a UNIX “shebang” line. In this case, the encoding declaration should be added as the second line of the file. For example:

#!/usr/bin/env python3# -*- coding: cp1252 -*-

第一行规则的有个例外,源代码以UNIX“shebang”行开始。在这种情况下,编码声明应该作为文件的第二行添加。例如:

#!/usr/bin/env python3# -*- coding: cp1252 -*-

Footnotes补录

1

On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously installed Python 2.x executable.

在Unix系统中,默认python 3.x解释器执行python命令没有安装,所以如果同时安装了python 2.x不会执行冲突。

译者说,好了,了解完python的基本信息后,从下一章开始我们将学习python基本数据类型——数字、字符串和列表。

上一篇 下一篇

猜你喜欢

热点阅读