Python,你了解么?

最近在考慮持續交付(Continuous Delivery)的一系列最佳實踐,持續集成工具的選擇影響到未來的投入,故而需要篩選一下。

使用 Java 和 Python 開發的持續集成工具比較普遍,我一向傾向于輕量級實現,便于學習、理解和演進。

之前對 Python 也偶有代碼接觸,朋友也經常聊起,但從沒有認真了解過。希望通過最近一段時間的努力,整個團隊開始踏上持續交付的旅程,進一步提升敏捷開發的價值。

Python is a programming language that lets you work quickly and integrate systems more effectively.

The best way to learn a language is to use it, play with the Python interpreter as you like.

社區

Python 社區活躍,推出新版本有節奏,文檔完善,遇到問題你幾乎總能找到合適的 Modules 和 Answers。社區是我很看重的一個因素,在這個演進非常快速的時代,借助于開源世界的力量,你的能力就不僅僅是倍增,可以說是站在巨人的肩膀之上。

Zen

Python 是一個有理想的語言,她有自己的 Zen,我覺得很不錯,信念,你懂么?
看看:Readability counts.

Creator

Python 的創建者 Guido van Rossum,有一個昵稱 BDFL (Benevolent Dictator For Life),在 Netherlands CWI 工作時創作了 Python。Rietveld - Code Review Web 工具 是這位仁兄的作品。

Style Guide for Python Code

風格指南有豐富的內容,二十多年來,不斷演進的標準庫的編碼約定,源自 Guido 的論文,Barry Warsaw 亦有貢獻。

文中對于可讀性、向后兼容性、一致性的闡述非常到位;尤其對于 一致性的見解,對可讀性和一致性沖突的處理等,更是充分體現了這個團隊水平之嫻熟,可以毫不夸張地說,這個語言的核心團隊值得尊敬。

Data model

要學習 Python,對其數據模型必須透徹了解。如果你是對 C/C++、PHP 等語言了解的程序員,那么了解 Python 和 這些語言之間的差異就更為重要了。
放下原來的邏輯框架和思考習慣,零起點,反倒有助于你快速切入和理解 Python。

Objects, values and types

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.)

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity.

  • 所有數據都以對象或者對象之間的關系來表示;
  • 對象具有ID、值、類型,對象一經創建,其ID和類型都是固定不變的,值可變的對象稱為 mutable,值不可變的對象稱為 immutable;
  • 對象的類型決定了該對象可有哪些操作,即類型決定行為;
  • 對象的類型定義了該對象可能有哪些值;

containers

Some objects contain references to other objects; these are called containers. Examples of containers are tuples (), lists [] and dictionaries {}.

對于 container 來說,理解 value、mutability 是需要有點功底的。

The standard type hierarchy

type definition
Numbers immutable integers, booleans, floating point numbers, and complex numbers
Strings immutable sequence A string is a sequence of values that represent Unicode code points.
Tuples () immutable sequence The items of a tuple are arbitrary Python objects
Bytes immutable sequence A bytes object is an immutable array. The items are 8-bit bytes
Lists [] mutable sequence The items of a list are arbitrary Python objects
Byte Arrays mutable sequence The items are 8-bit bytes
Sets {} mutable set set() constructor
Frozen sets {} immutable set frozenset() constructor
Dictionaries{} mutable mapping {key: value}

Sequences represent finite ordered sets indexed by non-negative numbers.

Set types represent unordered, finite sets of unique, immutable objects.

更多的 standard types,詳見 Built-in Types

Execution model

  • block
    Structure of a programm,程序結構的一個基本概念。和 scope 密切相關。
    請仔細了解 block 的各個相關概念。

  • Naming and binding

    Names refer to objects. Names are introduced by name binding operations.

    盡管在 Python 中,也會提到 variable ,但相對 variable,使用 name 更合適。
    對象是數據實體,name 只是一個指向對象的引用,是一個數據對象的名稱,并且這個名稱是需要依據上下文來解析的。

  • variable(變量)
    因為 mutable & immutable 的緣故,"變量"這個詞兒都有點尷尬。那就使用 name 吧。

  • scope(作用域)
    A scope defines the visibility of a name within a block.

一句話

object(對象)是數據實體,name 只是一個指向對象的引用,是所指向的數據對象的一個名稱。
理解了這句話,你就理解了 Python 數據模型的本質。
用 C++ 術語來表述的話,Python 的 name 就是 C++ 的引用或指針。

賦值就是一種 binding 操作;name 就是一個對 object 的引用

關于賦值、復制(shallow/deep copy)等的更多知識,可能會和數據結構相關了。

Python2 or Python3

Short version: Python 2.x is legacy, Python 3.x is the present and future of the language.

建議:
如果你是剛開始學習和應用 Python,則學習 Python 3 即可(2015-09-13 發布了 3.5.0)。
等到你基本熟悉了 Python 3 以后,可以再了解一下和 Python 2 的差異。
廣泛使用的 Twisted 當下還不支持 3.x(但已經取得了很大的進展),而 Buildbot 就使用 Twisted 在 Master 和 Slave 之間進行通信。

假如你要深入了解 Python,Nick Coghlan: Python 3 Q & A 這篇文章值得好好讀一讀。Nick Coghlan 是 CPython 的核心開發人員之一。

隨便摘錄一段:

While students in enterprise environments may still need to learn Python 2 for a few more years, there are some significant benefits in learning Python 3 first, as that means students will already know which concepts survived the transition, and be more naturally inclined to write code that fits into the common subset of Python 2 and Python 3. This approach will also encourage new Python users that need to use Python 2 for professional reasons to take advantage of the backports and other support modules on PyPI to bring their Python 2.x usage as close to writing Python 3 code as is practical.

參考

  1. 深入理解 python 中的賦值、引用、拷貝、作用域
  2. Immutable vs mutable types
  3. Python 3.5.0 documentation
  4. The Hitchhiker’s Guide to Python
  5. How to Choose Your First Programming Language

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容