【注】翻譯官方文檔,只是想在學(xué)習(xí)Python過程留下筆記,如涉及侵權(quán),請(qǐng)聯(lián)系刪除。
The Python Tutorial
linyk3簡(jiǎn)書:The Python Tutorial:Python教程筆記
linyk2655 Github LearnPython
原文:Using the Python Interpreter
- Using the Python Interpreter 使用Python 解釋器
2.1. Invoking the Interpreter 調(diào)用解釋器
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:
在能夠使用Python的機(jī)器中,Python解釋器經(jīng)常安裝在 /usr/local/bin/python3.7. 把 /usr/local/bin 這個(gè)目錄加在Unix shell 的搜索路徑,就能在Shell.輸入下面的命令來啟動(dòng):
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解釋器的路徑的選擇是安裝時(shí)的選項(xiàng),其他路徑也是可以的。可以讓你的Python大師或系統(tǒng)管理員來檢查。(例如,/usr/local/python 這個(gè)路徑是另外一個(gè)比較流行的選擇)
On Windows machines, the Python installation is usually placed in C:\Python36, though you can change this when you’re running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:
在Windows的機(jī)器中,Python經(jīng)常安裝在C:\Python36,即使你可以在執(zhí)行安裝程序的時(shí)候修改路徑。如果要把這個(gè)目錄加到環(huán)境變量中的Path中,你可以在Dos命令行提示器中輸入下面的命令:
set path=%path%;C:\python36
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().
在命令行提示器中輸入結(jié)束符(Unix是Control-D, Windows是Control-Z)可以讓解釋器以狀態(tài)‘0’退出。如果這個(gè)不起作用,你可以輸入下面的命令來退出解釋器: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.
在支持按行讀取的系統(tǒng)中,解釋器的行編輯的特征包括:交互式編輯、歷史替換以及代碼補(bǔ)全。獲取最快地檢查是否支持命令行編輯的方法就是在你的Python提示器中輸入Control-P. 如果它發(fā)出蜂鳴聲,說明支出命令行編輯。相關(guān)的介紹可以參考附錄Interactive Input Editing and History Substitution:交互式編輯和歷史替換。如果沒有任何事情發(fā)生,或者提示的是^P,說明命令行編輯是不支持的。這樣你只能使用退格鍵來刪除當(dāng)前行的字符。
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 scriptfrom that file.
解釋器的操作類似Unix Shell: 當(dāng)調(diào)用關(guān)聯(lián)于某個(gè)tty設(shè)備的標(biāo)準(zhǔn)輸入時(shí),它交互式的讀取并執(zhí)行命令。當(dāng)調(diào)用一個(gè)文件名為參數(shù)或者文件作為標(biāo)準(zhǔn)輸入時(shí),它讀取并執(zhí)行這個(gè)文件中的腳本。
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.
第二個(gè)啟動(dòng)解釋器的方式是:python -c command [arg] ..., 這樣可以執(zhí)行command 這個(gè)語句,類似shell 的 -c 選項(xiàng)。因?yàn)镻ython語句經(jīng)常包含有空格符或其他Shell中的特殊字符,所以建議將 command 整體用引號(hào)包起來。
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.
一些Python模塊也跟腳本一樣很有用。這些可以用python-m module [arg] ...命令來調(diào)用,就可以像在命令中把模塊的全名拼出來一樣來執(zhí)行module 這些模塊文件。
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.
當(dāng)一個(gè)腳本文件被使用,如果可以運(yùn)行腳本并且在后面輸入交互命令就更好了。這個(gè)可以通過在腳本前面加上-i來實(shí)現(xiàn)。
All command line options are described in Command line and environment.
所有的命令的選項(xiàng)都在 Command line and environment:命令行與運(yùn)行環(huán)境中有詳細(xì)介紹。
2.1.1. Argument Passing 參數(shù)傳遞
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 -ccommand 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.
我們知道解釋器中,腳本的名字及其后面附加的參數(shù)會(huì)被轉(zhuǎn)換為一個(gè)字符串列表并且賦值給sys模塊中的變量argv。 你可以通過執(zhí)行import sys來訪問這個(gè)列表。這個(gè)列表的長(zhǎng)度至少是1;當(dāng)沒有提供腳本或參數(shù)時(shí),sys.argv[0] 是一個(gè)空字符串。當(dāng)腳本的名字是'-'(表示標(biāo)準(zhǔn)輸入), sys.argv[0] 設(shè)置為 '-'. 當(dāng)使用 -c command 時(shí), sys.argv[0] 設(shè)置為 '-c'. 當(dāng)使用 -m module 時(shí), sys.argv[0] 被賦值為本地模塊的全稱. 在 -ccommand 或者 -m module 后面的選項(xiàng)不會(huì)被Python解釋器處理,而是留在 sys.argv 給 command or module 去處理。
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:
當(dāng)命令是從tty中讀取的,這時(shí)解釋器就是在所謂的交互模式。在這個(gè)模式中它為下一個(gè)命令提供了主要提示,經(jīng)常是三個(gè)大于號(hào)(>>>)來表示。為延續(xù)行提供了次級(jí)提示,缺省是三個(gè)點(diǎn)(...). 解釋器在開始它的第一個(gè)提示符之前,會(huì)打印歡迎信息,以其版本號(hào)和版權(quán)開始:
$ python3.7
Python 3.7 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
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 if statement:
延續(xù)行是在輸入一個(gè)多行的結(jié)構(gòu)時(shí)需要的。例如,可以看看if語句:
>>> 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.
更多關(guān)于交互模式,可以參考 Interactive Mode:交互模式.
2.2. The Interpreter and Its Environment 解釋器及其運(yùn)行環(huán)境
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.
默認(rèn)地,Python源文件都是UTF8編碼的。用這個(gè)編碼格式,世界上大部分的字符都可以同時(shí)來表示 字符串文字、標(biāo)識(shí)符和注釋 -- 雖然標(biāo)準(zhǔn)庫(kù)中的標(biāo)識(shí)符只使用了ACSII字符,遵從了可移植性代碼的便利性。為了能夠正確展示這些字符,你的編輯器必須能夠辨識(shí)出這個(gè)文件是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:
為了聲明一種非默認(rèn)的編碼格式,需要在文件的第一行中加上一個(gè)特殊的注釋,相關(guān)的語法就是:
# -*- coding: encoding -*-
where encoding is one of the valid codecs supported by Python.
encoding 是Python支持的的一個(gè)合法的codecs 。
For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:
例如,為了聲明使用的是 Windows-1252編碼格式,你的源文件中的第一行應(yīng)該是:
# -*- 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:
第一行規(guī)則的一個(gè)例外就是源文件以UNIX “shebang” line 開頭。在這種情況中,編碼格式的聲明應(yīng)該加在文件中的第二行。例如:
#!/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. |