VectorLu

《Python 基础教程》 学习笔记

《Python 基础教程》学习笔记,要点整理。

有些地方翻译有严重的错误,譬如异常一章最后应该是表达要去习惯使用 try/except,而不是“if/else”。没有怎么讲集合 set 的相关应用,其他的部分还不错(前 10 章还过得去)。类讲得很一般。另外还有些问题见: https://book.douban.com/review/5604726/

不过介绍了一些 Pythonic 的写法,对阅读他人的代码和写好 Python 有一些帮助。关于字符串和异常的一些细节讲得不错。

基础知识

3.0 之前的 Python 的除法,/ 结果与精度较高的操作数相同:

1
2
>>> 1/2
0

或者:

1
2
3
4
>>> from __future__ import division
>>1 / 2
0.5

round() 四舍五入为最接近的整数。

floor() 向下取整的函数,ceil() 向上取整:

1
2
3
4
5
>>> import math
>>> math.floor(32.9)
32.0
>>> math.ceil(32.9)
33.0

Python2 对于虚数的支持:

1
2
3
>>> import cmath
>>> cmath.sqrt(-1)
1j

即使不导入 cmath 模块,Python2 也是支持虚数运算的:

1
2
>>> (1+3j)*(9+4j)
(-3+31j)

保存并执行程序

用命令行执行 Python 脚本

1
$ python2 filename.py

如果你有多个版本的 Python,最好强调一下 Python 的版本究竟是 python2 还是 python3

让脚本像普通程序一样运行

如果希望运行普通程序一样运行 Python 脚本,在类 Unix 系统下,在代码的首行加入:

1
#!/usr/bin/env python2

在实际运行脚本之前,必须让脚本文件拥有可执行的属性,在命令行中首先:

1
$ chmod a+x helloPython2.py

然后就可以像执行普通程序一样执行它了:

1
$ helloPython2.py

如果执行不了的话,就试试:

1
$ ./helloPython2.py

甚至可以重命名程序,去掉 .py,执行:

1
$ ./hello

字符串

repr 会创建一个字符串,它以合法的 Python 表达式的形式来表示值。repr(x) 的功能也可以用一对反引号实现:

1
repr(x) == `x`

具体实例如下:

1
2
3
4
5
6
7
8
9
>>> temp = 42
>>> print "The temperature is " + temp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is " + `temp`
The temperature is 42
>>> print "The temperature is " + repr(temp)
The temperature is 42

不过 Python3.0 之后就不再用反引号了,因此,即使在旧代码中看到了反引号,也应该坚持用 repr()

input()raw_input()

input() 会假设用户输入的是合法的 Python 表达式:

1
2
3
4
5
6
7
8
9
10
11
12
>>> name = input('What is your name?\n')
What is your name?
Vector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Vector' is not defined
>>> name = input('What is your name?\n')
What is your name?
"Vector"
>>> name
'Vector'

要求用户带着引号输入他们的名字有点过分,因此,这就需要使用 raw_input(),它会把所有的输入当作原始数据,然后将其放入字符串中:

1
2
3
4
>>> name = raw_input("What is your name? ")
What is your name? Vector
>>> name
'Vector'

在 Python 3.0 之后:

1
2
3
4
5
>>> name = input('What is your name?\n')
What is your name?
Vector
>>> name
'Vector'

数据结构

通用序列操作

分片

步长不可以是 0,但步长可以是负数,即从右往左提取元素:

1
2
3
4
5
6
7
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 8, 9]
>>> numbers[8:3:-1]
[9, 8, 6, 5, 4]
>>> numbers[::-1]
[986543210]
>>> numbers[::-2]
[96420]

对于一个正数步长,Python 会从序列的头部开始向右提取元素(无论始末的索引是正数还是负数),直到最后一个元素(不包括);而对于负数步长,则是从序列的尾部开始向左提取元素,直到第一个元素(不包括)。

相加

相乘

in 判断

1
2
3
4
5
6
7
8
>>> permissions = 'rw'
>>> 'w' in permissions
True
>>> 'x' in permissions
False
>>> subject = '$$$ Get rich now!!! $$$'
>>> '$$$' in subject
True

前两个例子测试了 ‘w’ 和 ‘x’ 是否在字符串 permissions 中。在类 UNIX 系统中,这两行代码可以作为查看文件可写和可执行权限的脚本。最后一个例子可以作为垃圾邮件过滤器的一部分。

长度,最大值,最小值

len(), max(), min()

list

pop() 是唯一一个既能修改列表又能返回除了 None 之外的值的方法。

字符串

字符串格式化

如果右操作数是远足,其中每一个元素都会被单独格式化,每个值都需要一个对应的转换说明符。

1
2
3
4
5
6
>>> '%s plus %s equals %s' % (1, 1, 2)
'1 plus 1 equals 2'
>>> '%s plus %s equals %s' % 1, 1, 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

常用字符串方法

find() 方法

find() 可以在一个较长的字符串中查找子串,返回子串所在位置的最左端索引。如果没有找到则返回 -1

1
2
>>> 'With a moo-moo here, and a moo-moo there'.find('moo')
7

注意:find() 并不返回 boolean 值,如果返回 0,即在字符串开始处找到子串。

join() 方法

需要添加的队列元素都必须是字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> numSeqence = [1, 2, 3, 4, 5]
>>> sep = '+'
>>> sep.join(numSeqence) # 连接数字列表
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> strSequence = ['1', '2', '3', '4', '5']
>>> seq = '+'
>>> seq.join(strSequence)
'1+2+3+4+5'
>>> dirs = '', 'usr', 'bin', 'env'
>>> dirs
('', 'usr', 'bin', 'env')
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env

split() 方法

join() 的逆方法,将字符串分割成序列。不提供分隔符时,将所有空格作为分隔符。

1
2
3
4
5
6
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> '/usr/bin/env'.split('/')
['', 'usr', 'bin', 'env']
>>> 'Using the default'.split()
['Using', 'the', 'default']

###replace() 方法
返回某字符串的所有匹配项均被替换之后得到字符串。

1
2
>>> 'This is a test'.replace('is', 'eez')
'Theez eez a test'

strip() 方法

返回去除两侧(不包括内部)空格的字符串,它和 lower() 一起使用的话就可以很方便地对比输入的和存储的值。

1
2
3
4
5
6
7
>>> names = ['gumby''smith''jones']
>>> name = 'Gumby'
>>> name = 'Gumby '
>>> if name.lower().strip() in names: print 'Found it!'
...
Found it!

也可以去掉指定的字符,列为参数即可:

1
2
>>> '*** SPAM * for * everyone!!! ***'.strip(' *!')
'SPAM * for * everyone'

字典

使用

字典的格式化字符串

1
2
3
4
5
6
7
8
template = '''<html>
<head><title>%(title)s</title></head>
<body>
<h1>%(title)s</h1>
<p>%(text)s</p>
</body>'''
data = {'title': 'My Home Page', 'text': 'Welcome to my home page!'}
print template % data

执行结果:

1
2
3
4
5
6
7
$ python2 dataStructures.py
<html>
<head><title>My Home Page</title></head>
<body>
<h1>My Home Page</h1>
<p>Welcome to my home page!</p>
</body>

clear() 方法

清空字典

1
2
3
4
5
6
7
8
9
10
>>> d = {}
>>> d['name'] = 'Gumby'
>>> d['age'] = 42
>>> d
{'age'42'name''Gumby'}
>>> returned_value = d.clear()
>>> d
{}
>>> print returned_value
None

copy() 方法

浅拷贝,深拷贝要 from copy import deepcopy

fromkeys() 方法

使用给定的键建立新字典,每个键默认的对应值是 None,或者自己提供默认值:

1
2
3
4
>>> dict.fromkeys(['name''age'])
{'age': None, 'name': None}
>>> dict.fromkeys(['name''age'], '(unknow)')
{'age''(unknow)''name''(unknow)'}

get()setdefault()

当使用 get() 访问一个不存在的键时,没有任何异常,而得到了 None 值。还可以自定义 “默认”值,替换 None。如果键存在,就像普通的字典查询一样:

1
2
3
4
5
6
7
8
9
10
11
12
>>> d = {}
>>> print d['name']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'name'
>>> print d.get('name')
None
>>> d.get('name', 'N/A')
'N/A'
>>> d['name'] = 'Eric'
>>> d.get('name')
'Eric'

setdefault() 类似于 get(),如果字典不含给定键,可以设定相应的键值。

1
2
3
4
5
6
7
8
9
10
>>> d = {}
>>> d.setdefault('name''N/A')
'N/A'
>>> d
{'name''N/A'}
>>> d['name'] = 'Gumby'
>>> d.setdefault('name''N/A')
'Gumby'
>>> d
{'name''Gumby'}

items()iteritems()

后者往往更加高效:

1
2
3
4
5
6
7
8
9
10
11
12
>>> d = {'title''Python Web Site''url''http://www.python.org''spam'0}
>>> d.items()
[('url''http://www.python.org'), ('spam'0), ('title''Python Web Site')]
>>> it = d.iteritems()
>>> it
<dictionary-itemiterator object at 0x10f735cb0>
>>> for i in it:
...     print(i)
...
('url''http://www.python.org')
('spam'0)
('title''Python Web Site')

与之对应的还有 keys()iterkeys()values()itervalues()

pop()popitem()

1
2
3
4
5
>>> d = {'a':1, 'b':2}
>>> d.pop('a')
1
>>> d
{'b': 2}

popitem() 会随机弹出一个元素。

update() 方法

提供的字典项中的项会被添加到原来的字典中,如果有相同的键则会覆盖。

1
2
3
4
5
6
>>> d = {'a':1'b':2'c':3}
>>> new = {'c':99'd':100}
>>> d.update(new)
>>> d
{'a'1'c'99'b'2'd'100}

条件、循环和其他语句

导入

1
2
3
4
import somemodule
from somemodule import somefunction
from somemodule import somefunction, anotherfunction, yetanotherfunction
from somemodule import *

只有确定自己想要从给定的模块中导入所有功能时,才应该使用最后一个版本。但是如果两个模块都有 open(),只需用第一种方法导入,然后:

1
2
module1.open(...)
module2.open(...)

但是还可以在语句末尾增加 as 子句,在该子句后给出名字,或为整个模块提供别名:

1
2
import math as foobar
a = foobar.sqrt(4)

也可以为函数提供别名:

1
2
from module1 import open as open1
from module2 import open as open2

赋值魔法

序列解包

1
2
3
4
5
6
>>> x, y, z = 1, 2, 3
>>> x, y = y, x
>>> x
2
>>> y
1

事实上,这里所做的事情叫做 序列解包 (sequence unpacking)——将多个值的序列解开,然后放到变量的序列中。更形象一点地表示出来就是:

1
2
3
4
5
6
>>> values = 123
>>> values
(123)
>>> x, y, z = values
>>> x
1

当函数或者方法返回元组(或者其他序列或可迭代对象)时,这个特性尤其有用:

1
2
3
4
5
6
>>> d = {'name': 'Robin', 'girlfriend': 'Marion'}
>>> key, value = d.popitem()
>>> key
'girlfriend'
>>> value
'Marion'

它允许函数返回一个以上的值并且打包成元组,然后通过一个赋值语句很容易进行访问。所解包的序列中的元素数量必须和放置在赋值符号 = 左边的变量数量完全一致,否则 Python 会在赋值时引发异常:

1
2
3
4
5
6
7
8
9
>>> x, y, z = 1, 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2
)
>>> x, y = 1, 2, 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

注意:Python3 有另外一个解包的特性,可以像函数的参数列表一样使用星号运算符。

1
2
3
4
5
6
7
8
9
10
>>> a, b, *rest = [1, 2, 3, 4]
>>> rest
[3, 4]
>>> *rest, a, b = [1, 2, 3, 4]
>>> rest
[1, 2]
>>> a
3
>>> b
4

条件和条件语句

下面这些值在作为布尔表达式时,会被解释器看作 False

1
2
3
4
5
6
7
False
None
0 # 所有类型的数字 0,包括浮点型,长整型等
""
()
[]
{}

尽管上述都被认为是假值,也就是说 bool([]) == bool("")

避免将 is 运算符用于比较类似数值和字符串这类不可变值,由于 Python 内部操作这些对象的方式的原因,使用 is 运算符的结果是不可预测的。

一些迭代工具

并行迭代

内建的 zip() 可以用来进行并行迭代,可以把两个序列“压缩”在一起,然后返回一个元组的列表:

1
2
3
4
>>> names = ['anne', 'beth', 'george', 'damon']
>>> ages = [12, 45, 32, 102]
>>> zip(names, ages)
[('anne', 12), ('beth', 45), ('george', 32), ('damon', 102)]

zip() 可以作用于任意多的序列,并且可以应付不等长的序列——当最短的序列“用完”的时候就会停止。

编号迭代

enumerate() 可以在提供索引的地方迭代索引-值对,比如下面这个简化的屏蔽关键字的程序片段:

1
2
3
for indexstring in enumerate(strings):
if 'xxx' in string:
strings[index] = '[censores]'

翻转和排序迭代

reversed()sorted() 同列表的 reverse()sort() 方法类似,但作用于任何序列或可迭代对象上,不是原地修改对象,而是返回翻转或排序后的版本。sorted() 返回列表,reversed() 返回一个可迭代对象。

使用 break

1
2
3
4
while True:
word = raw_input('Please enter a word: ')
if not word: break
print 'The word was ' + word

循环中的 else 子句

1
2
3
4
5
6
7
8
from math import sqrt
for n in range(99, 81, -1):
root == sqrt(n)
if root == int(root):
print n
break
else:
print "Didn't find it"

列表推导式

1
2
3
girls = ['alice', 'bernice', 'clarice']
boys = ['chris', 'arnold', 'bob']
sameFirst = [b + '+' + g for b in boys for g in girls if b[0] == g[0]]

得到那些名字中的首字母相同的男孩和女孩。

由于它会检查每个可能的配对,这个方法效率不高,有一个更高效的方法。

1
2
3
4
5
6
girls = ['alice', 'bernice', 'clarice']
boys = ['chris', 'arnold', 'bob']
letterGirls = {}
for girl in girls:
letterGirls.setdefault(girl[0], []).append(girl)
print [b + '+' + g for b in boys for g in letterGirls[b[0]]]

其他

使用 del 语句不仅会移除一个对象的引用,也会移除那个名字本身。在 Python 中是没有办法删除值的,Python 解释器会用垃圾回收收回内存。

执行和求值字符串

执行一个字符串的语句是 exec(在 Python3.0 之后,exec() 是一个函数而不是语句):

1
2
>>> exec "print 'Hello world!'"
Hello world!

为了安全起见,可以增加一个字典,起到命名空间的作用。可以通过增加 in scope 来实现:

1
2
3
4
5
6
7
>>> from math import sqrt
>>> scope = {}
>>> exec 'sqrt = 1' in scope
>>> sqrt(4)
2.0
>>> scope['sqrt']
1

这样,潜在的破坏性代码并不会覆盖 sqrt(),原来的函数依然可以正常工作,而通过 exec 赋值的变量 sqrt 只在它的作用域内有效。

eval() 会计算 Python 表达式(以字符串形式书写),并且返回结果值:

1
2
3
>>> eval(raw_input("Enter an arithmetic expression: "))
Enter an arithmetic expression: 6 + 18 * 2
42

抽象

内建的 callable() 可以用来判断函数是否可调用:

1
2
3
4
5
6
7
>>> import math
>>> x = 1
>>> y = math.sqrt
>>> callable(x)
False
>>> callable(y)
True

在 Python3.0 之后,callable() 不再可用,需要用表达式 hasattr(func, __call__)

作用域

如果的确需要的话,可以用 globals()['globalParamName'] 来获取全局变量值,该函数的近亲是 vars(),可以返回全局变量的字典(locals() 返回局部变量的字典)。

1
2
3
4
5
6
>>def combine(parameter):
...     print parameter + globals()['parameter']
...
>>> parameter = 'berry'
>>> combine('Shrub')
Shrubberry

global x 告诉 Python x 是一个全局变量。

函数式编程

1
2
3
4
5
6
7
8
9
>>> map(str, range(10)) # Equivalent to [str(i) for i in r
ange(10)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> seq = ['foo', 'x41', '?!', '***']
>>> [x for x in seq if x.isalnum()]
['foo', 'x41']
>>> filter(lambda x: x.isalnum(), seq)
['foo''x41']

面向对象

面向对象最重要的优点:

  1. 继承 Inheritance:以普通的类为基础建立专门的类对象。
  2. 封装 Encapsulation:对外部世界隐藏对象的工作细节。
  3. 多态 Polimorphism:可以对不同类的对象使用同样的操作。

前面带有下划线的名字都不会被带星号的 import 语句 from module import * 导入。

子类可以将超类更加具体化,将其他类名写在 class 语句后的圆括号内可以指定超类:

1
2
3
4
5
6
7
8
9
class Filter:
def init(self):
self.blocked = []
def filter(self, sequence):
return [x for x in sequence if x not in self.blocked]
class SPAMFilter(Filter):
def init(self):
self.blocked = ['SPAM']

查看一个类是否是另一个的子类,可以使用内建的 issubclass()

1
2
3
4
>>> issubclass(SPAMFilter, Filter)
True
>>>issubclass(Filter, SPAMFilter)
False

用特殊属性 __bases__ 来查看已知类的基类们。
如果使用 __metaclass__ = type 或从 object 继承的方式来定义新式类,那么可以使用 type(s) 查看实例的类。

多重继承

谨慎使用!

如果一个方法从多个超类继承(也就是说有两个具有相同名字的不同方法),那么必须要注意超类的顺序:先继承的类中的方法会重写后继承的类中的方法。

异常

Python 用 异常对象 来表示异常情况。遇到错误后,会引发异常。如果异常对象并未被处理或捕捉,程序就会用所谓的 回溯(Traceback,一种错误信息)终止执行。

引发异常

为了引发异常,可以使用一个类(应该是 Exception 的子类)或者实例参数调用 raise 语句。使用类时,程序会自动创建实例。

1
2
3
4
5
6
7
8
9
>>> raise Exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception
>>> rasie Exception('hyperdrive overload')
File "<stdin>", line 1
rasie Exception('hyperdrive overload')
^
SyntaxError: invalid syntax

捕捉异常并处理

为了捕捉异常并且做出一些错误处理(本例中只是输出一些更友好的错误信息),可以:

1
2
3
4
5
6
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
except ZeroDivisionError:
print "The second number can't be zero!"

看起来用 if 语句检查 y 值更简单一些,本例中这样做的确很好,但是如果需要给程序加入更多的除法,那么就得给每个除法加个 if 语句,而使用 try/except 的话只需要一个错误处理器。

传递异常

用不带参数的 raise 来传递异常。在函数内引发异常,它就会被传递到函数被调用的地方。

考虑一下一个能处理 ZeroDivisionError 的计算器类。如果这个行为被激活,那么计算器就会打印错误信息,而不是让异常传播。如果在与用户进行交互的过程中使用,那么这就十分有必要。但是如果是在程序内部使用,引发异常会更利于程序做出相应处理——比如将错误抛出,让调用该段代码的地方处理该错误:

1
2
3
4
5
6
7
8
9
10
class MuffledCalculator:
muffled = False
def calc(self, expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print 'Division by 0 is illegal!'
else:
raise

多个 except 子句

异常处理不会搞乱原来的代码,而增加一大堆 if 语句检查可能的错误情况会让代码相当难读。

1
2
3
4
5
6
7
8
try:
x = input('Enter the first number: ')
y = input('Enter the second numver: ')
print x/y
except ZeroDivisionError:
print "The second number can't be zero!"
except TypeError:
print "That wasn't a number, was it?"

用一个块捕捉多个异常

用元组列出,注意不要忘记了元组的圆括号:

1
2
3
4
5
6
try:
x = input('Enter the first number: ')
y = input('Enter the second numver: ')
print x/y
except (ZeroDivisionError, TypeError, NameError):
print "..."

捕捉对象

如果希望在 except 子句中访问异常对象本身,可以使用两个参数(注意,就算要捕捉到多个异常,也只需向 except 子句提供一个参数——一个元组)。比如,如果想让程序继续运行,但是又因为某种原因想记录下错误(比如只是打印给用户看)。下面的示例程序会打印异常(如果发生的话),但是程序会继续运行:

1
2
3
4
5
6
7
8
try:
x = input('Enter the first number: ')
y = input('Enter the second numver: ')
print x/y
except (ZeroDivisionError, TypeError), e:
print e
print 'Hello, world!'

运行结果如下:

1
2
3
4
Enter the first number1
Enter the second numver: 0
integer division or modulo by zero
Hello, world!

注意
在 Python3.0 之后,except 子句写作 except (ZeroDivisionError, TypeError) as e:

全捕获

总有些异常无法被预料到,如果要捕获所有异常,可以在 except 子句中忽略所有的异常类

1
2
except:
print "..."

警告:
像这样捕捉所有异常是非常危险的,因为它会隐藏所有程序员未想到并且未做好准备处理的错误,它同样会捕捉用户终止执行的 Ctrl+C 企图,以及用 sys.exit 函数终止程序的企图,等等。这时用 except Exception, e 会更好些,或者对异常对象 e 进行一些检查。

万事大吉

可以给 try/except 语句加上 else 子句。如果没有引发异常,else 语句就会被执行。

1
2
3
4
5
6
try:
print 'A simple task'
except:
print 'What? Something went wrong?'
else:
print 'Ah...It went as planned.'

运行后会得到:

1
2
3
Root:Python2 $ python2 elseException.py
A simple task
Ah...It went as planned.

看下面这个示例:

1
2
3
4
5
6
7
8
9
10
11
while True:
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
value = x/y
print 'x/y is', value
except Exception e:
print 'Invalid input:', e
print 'Please try again'
else:
break

只要有错误发生,程序就会不断要求重新输入。

最后

无论是否发生异常,finally: 中的子句都会执行。

异常之禅

如果知道某段代码可能会导致某种异常,而又不希望程序以堆栈跟踪的形式终止,那么就根据需要添加 try/except 或者 try/finally 语句(或者它们的组合)进行处理。

有些时候,条件语句可以实现和异常处理同样的功能,但是条件语句可能在自然性和可读性上差血。而从另一方面来看,某些程序中使用 if/else 实现会比使用 try/except 要好。

假设有一个字典,我们希望打印出存储在特定的键下面的值。如果该键不存在,那么什么也不做。代码可能像下面这样写:

1
2
3
4
5
def describePerson(person):
print 'Description of', person['name']
print 'Age:', person['age']
if 'occupation' in person:
print 'Occupation:', person['occupation']

如果给程序提供包含名字 Throatwobbler Mangrove 和年龄 42(没有职业)的字典的函数,会得到如下输出:

1
2
Description of Throatwobbler Mangrove
Age: 42

如果添加了职业 camper,会得到如下输出:

1
2
3
Description of Throatwobbler Mangrove
Age: 42
Occupation: camper

代码非常直观,但是不够简洁。程序会两次查找 occupation 键——其中一次用来查看键是否存在(在条件语句中),另一次获得值。

另一个解决方案如下:

1
2
3
4
5
6
def describePerson(person):
print 'Description of', person['name']
print 'Age:', person['age']
try:
print 'Occupation: ' + person['occupation']
except KeyError: pass

注意:
这里打印职业时用的 + 而不是 ,,否则字符串 'Occupation: 在异常引发之前就会被输出。

try/except 语句比 if/else 语句更加 Pythonic。

请求宽恕易于请求许可。
在做一件事时去处理可能发生的错误,而不是在开始做事之前做大量的检查。

魔法方法

重写构造方法

推荐使用第二种——Python3.0 之后普遍使用的方法。

调用未绑定的超类构造方法

使用 ParentClass.__init__(self)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print 'Aaaah...'
self.hungry = False
else:
print 'No, thanks!'
b = Bird()
b.eat()
b.eat()
print "---------------"
class SongBird(Bird):
def __init__(self):
Bird.__init__(self)
self.sound = 'Squaqk!'
def sing(self):
print self.sound
sb = SongBird()
sb.sing()
sb.eat()
sb.eat()

运行结果:

1
2
3
4
5
6
7
$ python2 magicMethod.py
Aaaah...
No, thanks!
---------------
Squaqk!
Aaaah...
No, thanks!

使用 super()

使用 super(CurrentClass, self)。该函数返回对象的任何方法都是父类的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
__metaclass__ = type # super() 只在新式类中起作用
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print 'Aaaah...'
self.hungry = False
else:
print 'No, thanks!'
b = Bird()
b.eat()
b.eat()
print "---------------"
class SongBird(Bird):
def __init__(self):
super(SongBird, self).__init__()
self.sound = 'Squaqk!'
def sing(self):
print self.sound
sb = SongBird()
sb.sing()
sb.eat()
sb.eat()

Python3.0 之后,super() 可以不用任何参数。

super() 返回一个 super 对象,这个对象负责进行方法解析,当对其特性进行访问时,它会查找所有超类(以及超类的超类),直到找到所需的特性为止(或者引发一个 AttributeError 异常)。

成员访问

基本的序列和映射规则

The word protocol is often used in Python to describe the rules governing some form of behavior. This is somewhat similar to the notion of interfaces. The protocol says something about which methods you should implement and what those methods should do. Because polymorphism in Python is based on only the object’s behavior (and not on its ancestry, for example, its class or superclass, and so forth), this is an important concept: where other languages might require an object to a certain class or to implement a certain interface, Python often simply requires it to follow some given protocol. So, to be a sequence, all you have to do is follow the sequence protocol.

protocol 说明了应该实现何种方法和这些方法应该做什么。比如只需要遵守序列给定的 protocol,就可以成为一个序列。

序列和映射是对象的集合。为了实现它们基本的规则,如果对象是不可变的,那么就需要使用两个魔法方法,如果是可变的则需要使用 4 个。

  1. __len__(self):返回集合中所含项目的数量。如果 __len__ 返回 0(并且没有实现重写该行为的 __nonzero__)对象会被当作一个布尔变量中的假值(空的列表,元组,字符串和字典等)进行处理。

  2. __getitem__(self, key):这个方法返回与所给键对应的值。对于序列,键是 0n-1 的整数(或相应的负数,x[-n]x[len(x)-n] 一样),对于映射来说,键更加自由。

  3. __setitem__(self, key, value)
  4. __delitem__(self, key):在对一部分对象使用 del 语句时被调用,同时必须删除和元素相关的键。这个方法也是为可修改的对象定义的。

附加要求:

  1. 如果键是不合适的类型(例如,对序列使用字符串作为键),会引发一个 TypeError 异常。
  2. 如果序列的索引是正确的类型,但超出了范围,应该引发一个 IndexError

实现一个理论上无限大的算术序列:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def checkIndex(key):
"""
Is the given key an acceptable index?
To be acceptable, the key should be a non-negative integer.
If it is not an integer, a TypeError is raised;
if it is negative, an IndexError is raised
(since the sequence is of infinite lenth).
"""
if not isinstance(key, (int, long)): raise TypeError
if key < 0: raise IndexError
class ArithmeticSequence:
def __init__(self, start = 0, step = 1):
"""
Initialize the arithmetic sequence.
start - the first value in the sequence
step - the difference between two adjacent values
changed - a dictionary of values that have been modified by the user
"""
self.start = start
self.step = step
self.changed = {}
def __getitem__(self, key):
"""
Get an item from the arithmetic sequence.
"""
checkIndex(key)
try: return self.changed[key] # Modifies?
except KeyError:
return self.start + key * self.step
def __setitem__(self, key, value):
"""
Change an item in the arithmetic sequence.
"""
checkIndex(key)
self.changed[key] = value
if __name__ == '__main__':
s = ArithmeticSequence(1, 2)
print s[4]
s[4] = 2
print s[4]
print s[5]
del s[4]

没有实现 __del__() 的原因是希望删除元素是非法的,执行结果:

1
2
3
4
5
6
7
9
2
11
Traceback (most recent call last):
  File "sequenceProtocol.py", line 47, in <module>
    del s[4]
AttributeError: ArithmeticSequence instance has no attribute '__delitem__'

一般避免使用 isinstance() 函数(因为类或类型检查与 Python 中多态的目标背道而驰)。因为 Python 的 language reference 上明确指出索引必须是整数(包括长整数),所以上面的代码才会如此使用。遵守标准是使用类型检查的(很少的)正当理由之一。

属性

property() 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
__metaclass__ = type
class Rectangle:
def __init__(self):
self.width = 0
self.height = 0
def setSize(self, size):
self.width, self.height = size
def getSize(self):
return self.width, self.height
size = property(getSize, setSize)
if __name__ == '__main__':
r = Rectangle()
r.width = 10
r.height = 5
print r.size
r.size = 150, 100
print r.width, r.height

执行结果

1
2
(10, 5)
150 100

静态方法和类成员方法

静态方法的定义没有 self 参数,且能够被类本身直接调用。类方法在定义hi需要名为 cls 的类似于 self 的参数,可以直接被类调用,cls 参数是自动被绑定到类的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
__metaclass__ = type
class MyClass:
def smeth():
print 'This is a static method'
smeth = staticmethod(smeth)
def cmeth(cls):
print 'This is a class method of', cls
cmeth = classmethod(cmeth)
MyClass.smeth()
MyClass.cmeth()
a = MyClass()
a.smeth()
a.cmeth()

手动包装和替换方法的技术看起来有点单调,在 Python2.4 之后,引入了叫作 装饰器 的新语法。使用艾特操作符 @,在方法(或函数)的的上方将装饰起列出,从而指定一个或者更多的装饰器(多个装饰器在应用时的顺序与指定顺序相反)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
__metaclass__ = type
class MyClass:
@staticmethod
def smeth():
print 'This is a static method'
# smeth = staticmethod(smeth)
@classmethod
def cmeth(cls):
print 'This is a class method of', cls
# cmeth = classmethod(cmeth)
MyClass.smeth()
MyClass.cmeth()
a = MyClass()
a.smeth()
a.cmeth()

执行结果:

1
2
3
4
This is a static method
This is a class method of <class '__main__.MyClass'>
This is a static method
This is a class method of <class '__main__.MyClass'>

__getattr__(), __setattr__()

为了在访问特性的时候可以执行代码,必须使用一些魔法方法。

  • __getattribute__(self, name):当特性 name 被访问时自动被调用(只能在新式类中使用)。
  • __getattr__(self, name):当特性 name 被访问且对象没有相应的特性时被自动调用。
  • __setattrj__(self, name, value):当试图给特性 name 赋值时会被自动调用。
  • __delattr__(self, name): 当试图删除特性 name 时被自动调用。

尽管和使用 property 相比有点复杂(而且在某些方面效率更低),但这特殊方法是很强大的,因为可以对处理很多属性的方法进行再编码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Rectangle:
def __init__(self):
self.width = 0
self.height = 0
def __setattr__(self, name, value):
if name == 'size':
self.width, self, height = value
else:
self.__dict__[name] = value
def __getattr__(self, name):
if name == 'size':
return self.width, self.height
else:
raise AttributeError

注意:

  1. __setattr__() 方法在所涉及到的属性不是 size 时也会被调用。因此,这个方法必须考虑新增属性的情况。为了避免 __setattr__() 方法被再次调用(从而使程序陷入死循环),__dict__() 被用来代替普通的属性赋值操作。
  2. __getattr__() 只在普通的属性没有被找到的时候调用,这就是说如果给定的名字不是 size,这个特性不存在,这个方法会引发一个 AttributeError 异常。

就像死循环陷阱和 __setattr__() 有关系,还有一个陷阱和 __getattribute__() 有关系。因为 __getattribute__() 拦截所有特性的访问,也拦截对 __dict__ 的访问!访问 __getattribute__() 中与 self 相关的特性时,使用超类的 __getattribute__() 方法(使用 super())是唯一安全的途径。

迭代器

迭代器规则

迭代——重复做一些事很多次。到现在为止只是在 for 循环中对序列和字典进行迭代,但实际上也能对其他的对象进行迭代:实现 __iter__() 方法的对象。

__iter__() 返回一个迭代器 iterator,所谓的迭代器就是具有 next() 的对象。在调用 next() 时,迭代器会返回它的下一个值。如果 next() 方法被调用,但迭代器没有值可以返回,就会引发 StopIteration 异常。

在 Python3.0 之后,迭代器对象应该实现 __next__(),而不是 next()

一个实现了 __iter__() 方法的对象是可迭代的,一个实现了 next() 方法的对象则是迭代器。

迭代可以不必一次性将所有值算出并存储,而是可以一个接一个地计算值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Fibs:
def __init__(self):
self.a = 0
self.b = 1
def next(self):
self.a, self.b = self.b, self.a+self.b
return self.a
def __iter__(self):
return self
if __name__ == '__main__':
fibs = Fibs()
for f in fibs:
if f > 1000:
print f
break

内建函数 iter() 可以从可迭代对象中获得迭代器:

1
2
3
it = iter([1, 2, 3])
print it.next()
print it.next()

当然也可以用 list() 构造方法显示地将迭代器转换为列表。另外,在 Python3.0 之后,应该用 print(next(it))

生成器

用圆括号返回生成器

1
2
g = ((i+2)**2 for i in range(2, 39))
g.next()

yield

1
2
3
4
5
def nested = [[1, 2], [3, 4], [5]]
def flatten(nested):
for sublist in nested:
for element in sublist:
yield element

任意包含 yield 语句的函数成为生成器。它不是像 return 那样返回值,而是每次产生多个值。

递归生成器

上一节创建的生成器只能处理两层嵌套,为了处理嵌套使用了两个 for 循环。如果要处理任意层的嵌套该怎么办?例如,可能要使用其来表示树形结构。每层嵌套需要增加一个 for 循环,但因为不知道有几层嵌套,所以必须把解决方案变得更灵活——求助于 递归

1
2
3
4
5
6
7
8
9
10
11
12
13
# DFS 的一种体现
# 不能用于元素类型是 str 等可迭代类型的列表
def flatten1(nested):
try:
for sublist in nested:
for element in flatten(sublist):
yield element
except TypeError:
yield nested
nestedL = [[[1], 2], 3, 4, [5, [6, 7]], 8]
flattenedList = list(flatten(nestedL))
print flattenedList

There are two reasons why you shouldn’t iterate over string-like objects in the flatten function. First, you want to treat string-like objects as atomic values, not as sequences that should be flattened. Second, iterating over them would actually lead to infinite recursion because the first element of a string is another string of length one, and the first element of that string is the string itself!

为了处理上述问题,则必须在生成器开始处添加一个检查语句。试着将传入的对象和一个字符串拼接,看看会不会出现 TypeError,这是检查一个对象是不是 string-like 的最简单、最快速的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def flatten2(nested):
try:
# 不迭代 string-like 的对象
try:
nested + ''
except TypeError:
pass
else:
raise TypeError
for sublist in nested:
for element in flatten2(sublist):
yield element
except TypeError:
yield nested

如果表达式 nested + '' 引发了一个 TypeError,它就会被忽略。然而如果没有引发 TypeError,那么内层 try 语句中的 else 子句会引发一个它自己的 TypeError 异常。继而完整地返回这个字符串,而不是遍历该字符串。

更多关于生成器方法的信息

八皇后问题

完整程序点这里查看

有一个棋盘和 8 个要放到上面的皇后,唯一的要求皇后之间不能形成威胁,也就是说任意两个皇后不能在同一行,不能在同一列,也不能在同一条斜线上。

这是一个经典的回溯问题,首先尝试放置第一个皇后(在第一行),然后放置第二个,依此类推。如果发现不能放置下一个皇后,就回溯到上一步,试着将皇后放到其他的位置。最后,或者尝试完所有的可能或者找到解决方案。

状态表示

用一个元组来表示皇后的位置,比如 state[0] == 3,那么就表示在第 1 行的皇后是在第 4 列。

寻找冲突

已知的皇后的位置被传递给 conflict()(以状态元组的形式),然后由函数判断下一个皇后的位置会不会有新的冲突。

1
2
3
4
5
6
def conflict(state, nextX):
nextY = len(state)
for i in range(nextY):
if abs(state[i]-nextX) in (0, nextY-i):
return True
return False

abs(state[i]-nextX) in (0, nextY-i) 表示如果下一个皇后和正在考虑的前一个皇后的水平距离为 0(列相同)或者等于垂直距离(在一条对角线上)就返回 True,否则就返回 False

Base Case

最后一个皇后,根据其他皇后的位置生成它自己能占据的所有位置(可能没有)。

1
2
3
4
5
def queens(num, state):
if len(state) == num - 1:
for pos in range(num):
if not conflict(state, pos):
yield pos

需要递归的情况

在完成基本情况时,递归函数会假定(通过归纳)所有的来自之前位置的结果都是可行的,因此这里要做的就是为前面的 queens()if len(state) == num - 1 增加 else 子句。

那么递归调用返回什么结果呢?

想得到的是已确定的所有的皇后的位置,假定将位置信息作为一个元组返回。在这种情况下,需要修改基本情况也返回一个长度为 1 的元组。

这样,程序从递归调用的返回值得到已确定的所有皇后的位置(用元组所表示),从而为后面的皇后提供相应的合法位置信息,并且将当前合法的位置信息添加到元组中传递给后面的皇后。

1
2
3
4
5
else:
for pos in range(num):
if not conflict(state, pos):
for result in queens(num, state + (pos,)):
yield (pos,) + result

for posif not conflict 部分和前面的代码相同,因此可以稍微简化一下代码。添加一些默认的参数:

1
2
3
4
5
6
7
8
def queens(num=8, state=()):
for pos in range(num):
if not conflict(state, pos):
if len(state) == num-1:
yield (pos, )
else:
for result in queens(num, state + (pos, )):
yield (pos, ) + result

注意 queens(num, state + (pos, ))yield (pos, ) + result 中的 pos 在加法中在前还是在后。

queens(num, state + (pos, ))state 是已确定的状态,pos 是新加入的位置信息,(pos, )state 后面。

yield (pos, ) + result 中,由于递归调用,最先返回的 result 是最后一行的皇后位置,向前递推,故 posresult 前面。

当然这个方法的效率不高,不过是一个很好的递归回溯,深度优先的范例,完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def conflict(state, nextX):
nextY = len(state)
for i in range(nextY):
if abs(state[i]-nextX) in (0, nextY-i):
return True
return False
def queens(num=8, state=()):
for pos in range(num):
if not conflict(state, pos):
if len(state) == num -1:
yield (pos, )
else:
for result in queens(num, state + (pos, )):
yield (pos, ) + result
def prettyPrint(solution):
def line(pos, length=len(solution)):
return '. '*(pos) + 'X ' + '. '*(length-pos-1)
for pos in solution:
print line(pos)
if __name__ == '__main__':
import random
prettyPrint(random.choice(list(queens(8))))
for solution in queens(8):
print solution

下面是 网友做的一行解决八皇后问题

1
_=[__import__('sys').stdout.write("\n".join('.' * i + 'Q' + '.' * (8-i-1) for i in vec) + "\n===\n") for vec in __import__('itertools').permutations(xrange(8)) if 8 == len(set(vec[i]+i for i in xrange(8))) == len(set(vec[i]-i for i in xrange(8)))]

Batteries Included

这里中文版翻译有问题:标题 Batteries Included 不是什么“充电时刻”,是:

The Python source distribution has long maintained the philosophy of “batteries included” – having a rich and versatile standard library which is immediately available, without making the user download separate packages. This gives the Python language a head start in many projects.

摘自 PEP 206 – Python Advanced Library

模块中有什么

dir() 函数

返回一个模块所有变量、函数、类等等:

1
2
3
>>> import copy
>>> dir(copy)
['Error', 'PyStringMap', '_EmptyClass', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_dispatch', '_copy_immutable', '_copy_inst', '_copy_with_constructor', '_copy_with_copy_method', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_inst', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', '_test', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']

__all__ 是什么

是对外的接口,如果导入 from copy import * 实际上就是导入的:

1
2
3
>>> import copy
>>> copy.__all__
['Error', 'copy', 'deepcopy']

如果想要导入如 PyStringMap,需要用 from copy import PyStringMap 专门导入才行。

测试

测试驱动开发

覆盖度工具,比如 trace.py 程序,可以使用 import trace 导入,使用 help(trace) 查看帮助。

过程如下:

  1. 指出需要的新特性。记录下来,然后为其编写一个测试。
  2. 编写特性的概要代码,这样程序就可以运行而没有任何语法等方面的错误,但是测试会失败。看到测试失败是很重要的,这样就能确定测试 可以失败(因为如果测试代码本身有问题,可能导致无论什么情况下,测试都会成功)。
  3. 为特性的概要编写 dummy code,能满足测试要求就行。不用准确地实现功能,只要保证测试可以通过即可。这样就可以保证开发时总是能通过测试了。
  4. 完成代码。删除 dummy code

单元测试工具

  • doctest:用来检查文档
  • unittest:通用测试框架

黄金法则 “使其工作、使其更好、使其更快”——单元测试让程序可以工作,源代码检查让程序更好,最后,性能分析会让程序更快。

源代码检查工具

  • PyChecker
  • PyLint

分析

标准库中包含了一个叫 profile 的分析模块,还有一个更快的嵌入式 C 语言版本,叫作 hotshot(Python3 中相应的替代模块叫作 cProfile)。使用分析程序非常简单:

1
2
3
import profile
from my_math import product
profile.run('product(1, 2)')
您的支持将鼓励我继续创作!

热评文章