python我爱编程

Python,你了解么?

2015-10-17  本文已影响159人  michael_jia

最近在考虑持续交付(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.

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

一句话

object(对象)是数据实体,name 只是一个指向对象的引用,是所指向的数据对象的一个名称。
理解了这句话,你就理解了 Python 数据模型的本质。
用 C++ 术语来表述的话,Python 的 name 就是 C++ 的引用或指针。

赋值就是一种 binding 操作;name 就是一个对 object 的引用.png

关于赋值、复制(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

上一篇 下一篇

猜你喜欢

热点阅读