If you ever wrote a large shell script, you probably know this feeling: you'd love to add yet another feature, but it's already so slow, and so big, and so complicated; or the feature involves a system call or other function that is only accessible from C ...Usually the problem at hand isn't serious enough to warrant rewriting the script in C; perhaps the problem requires variable-length strings or other data types (like sorted lists of file names) that are easy in the shell but lots of work to implement in C, or perhaps you're not sufficiently familiar with C.
如果你寫過大規(guī)模的 Shell 腳本,應(yīng)該會有過這樣的體會:你還非常想再加一些別的功能進去,但它已經(jīng)太大、太慢、太復(fù)雜了;或者這個功能需要調(diào)用一個系統(tǒng)函數(shù),或者它只適合通過 C 來調(diào)用 ...通常這些問題還不足以嚴肅到需要用 C 重寫這個腳本;可能這個功能需要一些類似變長字符串或其它一些在 Shell 腳本中很容易找到的數(shù)據(jù)類型(比如文件名的有序列表),但它們用 C 來實現(xiàn)就要做大量的工作,或者,你對 C 還不是很熟悉。
Another situation: perhaps you have to work with several C libraries, and the usual C write/compile/test/re-compile cycle is too slow. You need to develop software more quickly. Possibly perhaps you've written a program that could use an extension language, and you don't want to design a language, write and debug an interpreter for it, then tie it into your application.
另一種情況:可能你需要使用幾個 C 庫來工作,通常 C 的編寫/編譯/測試/重編譯周期太慢。你需要盡快的開發(fā)軟件。也許你需要寫一個使用擴展語言的程序,但不想設(shè)計一個語言,并為此編寫調(diào)試一個解釋器,然后再把它集成進你的程序。
In such cases, Python may be just the language for you. Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than the shell has. On the other hand, it also offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries that would cost you days to implement efficiently in C. Because of its more general data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.
遇到以上情況,Python 可能就是你要找的語言。Python 很容易上手,但它是一門真正的編程語言,相對于 Shell,它提供的針對大型程序的支持和結(jié)構(gòu)要多的多。另一方面,它提供了比 C 更多的錯誤檢查,并且,做為一門高級語言,它擁有內(nèi)置的高級數(shù)據(jù)類型,例如可變數(shù)組和字典,如果通過 C 來實現(xiàn)的話,這些工作可能讓你大干上幾天的時間。因為擁有更多的通用數(shù)據(jù)類型,Python 適合比 Awk 甚至 Perl 更廣泛的問題領(lǐng)域,在其它的很多領(lǐng)域,Python 至少比別的語言要易用得多。
Python allows you to split up your program in modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs -- or as examples to start learning to program in Python. There are also built-in modules that provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.
Python 可以讓你把自己的程序分隔成不同的模塊,以便在其它的 Python 程序中重用。這樣你就可以讓自己的程序基于一個很大的標(biāo)準(zhǔn)模塊集或者用它們做為示例來學(xué)習(xí) Python 編程。Python 中集成了一些類似文件 I/O,系統(tǒng)調(diào)用,sockets,甚至像 Tk 這樣的用戶圖形接口。
Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.
Python是一門解釋型語言,因為不需要編譯和鏈接的時間,它可以幫你省下一些開發(fā)時間。解釋器可以交互式使用,這樣就可以很方便的測試語言中的各種功能,以便于編寫發(fā)布用的程序,或者進行自下而上的開發(fā)。還可以當(dāng)它是一個隨手可用的計算器。
Python allows writing very compact and readable programs. Programs written in Python are typically much shorter than equivalent C or C++ programs, for several reasons: Python 可以寫出很緊湊和可讀性很強的程序。用 Python 寫的程序通常比同樣的 C 或 C++ 程序要短得多,這是因為以下幾個原因:
高級數(shù)據(jù)結(jié)構(gòu)使你可以在一個單獨的語句中表達出很復(fù)雜的操作; 語句的組織依賴于縮進而不是 begin/end 塊; 不需要變量或參數(shù)聲明。
Python is extensible: if you know how to program in C it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed, or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.
Python 是 可擴展的:如果你會用 C 語言寫程序,那就可以很容易的為解釋器添加新的集成模塊和功能,或者優(yōu)化瓶頸,使其達到最大速度,或者使 Python 能夠鏈接到所需的二進制架構(gòu)上(比如某個專用的商業(yè)圖形庫)。等你真正熟悉這一切了,你就可以把 Python 集成進由 C 寫成的程序,把 Python 當(dāng)做這個程序的擴展或命令行語言。
By the way, the language is named after the BBC show ``Monty Python's Flying Circus'' and has nothing to do with nasty reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!
順便說一下,這個語言的名字來源于 BBC 的“Monty Python's Flying Circus”節(jié)目,和兇猛的爬蟲沒有任何關(guān)系。在文檔中引用 Monty Python 典故不僅是允許的,而且還受到鼓勵!
Now that you are all excited about Python, you'll want to examine it in some more detail. Since the best way to learn a language is using it, you are invited here to do so.
現(xiàn)在你已經(jīng)了解了 Python 中所有激動人心的東西,大概你想仔細的試試它了。學(xué)習(xí)一門語言最好的辦法就是使用它,你會很樂于這樣做。
In the next chapter, the mechanics of using the interpreter are explained. This is rather mundane information, but essential for trying out the examples shown later.
下一節(jié)中,我們會很機械的說明解釋器的用法。這沒有什么神秘的,不過有助于我們練習(xí)后面展示的例子。
The rest of the tutorial introduces various features of the Python language and system through examples, beginning with simple expressions, statements and data types, through functions and modules, and finally touching upon advanced concepts like exceptions and user-defined classes.
本指南其它部分通過例子介紹了 Python 語言和系統(tǒng)的各種功能,開始是簡單表達式、語法和數(shù)據(jù)類型,接下來是函數(shù)和模塊,最后是諸如異常和自定義類這樣的高級內(nèi)容。
譯者:劉鑫(march.liu AT gmail DOT com) 由:limodou轉(zhuǎn)(limodou AT gmail DOT com) CHM 文件制作:Colin.Wang 2007年9月