1 异常:
2 dir(module) 列出模块的内容
3
4 >>> raise ArithmeticError
5
6 Traceback (most recent call last):
7 File "<pyshell#2>", line 1, in <module>
8 raise ArithmeticError
9 ArithmeticError
10
11 >>> raise ArithmeticError("Error occured!")
12
13 Traceback (most recent call last):
14 File "<pyshell#3>", line 1, in <module>
15 raise ArithmeticError("Error occured!")
16 ArithmeticError: Error occured!
17
18 try:
19 xxx
20 except <some type of exception>:
21 xxx
22 except:
23 print "something wrong happened."
24 else:
25 run normal code...
26 finally: #2.5以后可以组合使用
27 code which must be excuted...
28
29 魔法方法、属性和迭代器
30 super函数:(sample)
31 class Bird(object):
32 def __init__(self):
33 self.hungry = True
34 def eat(self):
35 if self.hungry:
36 print 'Aaaah...'
37 self.hungry = False
38 else:
39 print 'No, thanks!'
40
41 class SongBird(Bird):
42 def __init__(self):
43 self.song = 'Lalala.'
44 Bird.__init__(self)
45 def sing(self):
46 print self.song
47
48 class NewBird(Bird):
49 def __init__(self):
50 super(NewBird, self).__init__()
51 self.attr = 'New type.'
52 def show(self):
53 print self.attr
54
55 s = SongBird()
56 s.eat()
57 s.eat()
58 s.sing()
59
60 n = NewBird()
61 print n.hungry
62 n.eat()
63 n.eat()
64 n.show()
65
66 魔法方法:
67 __init__(self):构造函数
68
69 基本的序列和映射规则
70 __len__(self):返回集合中所含项目的数量
71 __getitem__(self, key):返回与所给键对应的值
72 __setitem__(self, key, value):按一定的方式存储和key相关的value
73 __delitem__(self, key):在对一部分对象使用del语句时被调用,同时必须删除和元素相关的键
74
75 子类化内建类型
76
77 访问器:getxxx & setxxx
78
79 属性:
80 class Rectangle(object):
81 def __init__(self):
82 self.width = 0
83 self.height = 0
84 def setSize(self, size):
85 self.width, self.height = size
86 def getSize(self):
87 return self.width, self.height
88 size = property(getSize, setSize)
89
90 r = Rectangle()
91 r.setSize((1, 2))
92 print r.getSize()
93 print r.size
94 r.size = 3, 4
95 print r.size
96 r.size = (5, 6)
97 print r.size
98 ...
99 (1, 2)
100 (1, 2)
101 (3, 4)
102 (5, 6)
103
104 静态方法、类成员方法:
105 class MyClass(object):
106
107 @staticmethod
108 def smethod():
109 print 'Static method.'
110
111 @classmethod
112 def cmethod(cls):
113 print 'Class method of ', cls
114
115 MyClass.smethod()
116 MyClass.cmethod()
117 ...
118 Static method.
119 Class method of <class '__main__.MyClass'>
120
121 迭代器:
122 class Fibs(object):
123 def __init__(self):
124 self.a = 0
125 self.b = 1
126 def next(self):
127 self.a, self.b = self.b, self.a + self.b
128 if self.a > 200:
129 raise StopIteration
130 return self.a
131 def __iter__(self):
132 return self
133
134 f = Fibs()
135 print f.next()
136 print f.next(), '\n'
137 nums = []
138 for i in f:
139 if i > 100:
140 break
141 nums.append(i)
142 print nums
143 print tuple(nums)
144
145 l = Fibs()
146 print list(l)
147 ...
148 1
149 1
150
151 [2, 3, 5, 8, 13, 21, 34, 55, 89]
152 (2, 3, 5, 8, 13, 21, 34, 55, 89)
153 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
154
155 生成器:生成器的函数 yield & 生成器的迭代器
156 def flatten(lol):
157 for sublist in lol:
158 for element in sublist:
159 yield element
160 lol = [[1, 2, 3], [4, 5, 6]]
161 result = []
162 for element in flatten(lol):
163 result.append(element)
164 print result
165 print list(flatten(lol))
166 print tuple(flatten(lol))
167
168 列表推导式:
169 >>> [x*x for x in range(20) if x % 2 == 1 or x % 5 == 0]
170 [0, 1, 9, 25, 49, 81, 100, 121, 169, 225, 289, 361]
171
172 pass语句:
173 pass语句什么都不做.它只在语法上需要一条语句但程序不需要任何操作时使用
174
175 del语句:
176 删除不再使用的对象
177
178 exec & eval:
179 执行和求职字符串
180
181 \n:换行
182 \r:回车
183
184 模块:
185 dir:查看模块包含的内容
186 __all__变量:定义模块的共有接口
187 help(module.method):获取帮助信息,来自__doc__ and some other information
188 __file__变量:源码位置
189
190 重要的模块:
191 sys
192 os
193 webbrowser
194 >>> import webbrowser
195 >>> webbrowser.open("http://www.google.com")
196 True
197 fileinput
198 sets:集合
199 heapq:堆
200 collections模块deque类:双端队列
201 time:模块
202 random:模块
203 shelve:数据存储
204 re模块:
205 正则表达式
206 通配符:.
207 转义字符:\
208 两个级别转义:通过解释器转义;通过re模块转义
209 字符集:[a-zA-z0-9]
210 反转字符集:[^abc]
211 ^:脱字符
212 选择符:|
213 子模式:(subpattern)
214 可选项:(pattern)?
215 重复子模式:(pattern)*, (pattern)+, (pattern){m, n}
216 仅匹配开始或结尾:^pattern, $pattern