循环设计:
循环对象
1
2
3
4
5
6
7
8
9
10
11>>> f = open('test.txt')
>>> f.next()
'123\n'
>>> f.next()
'456\n'
>>> f.next()
'789\n'
>>> f.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration迭代器:
1
2
3
4
5
6
7
8
9
10
11
12>>> a = [1,2,3]
>>> b = iter(a)
>>> b.next()
1
>>> b.next()
2
>>> b.next()
3
>>> b.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration生成器:
1
2
3
4
5
6
7
8
9
10
11
12
13>>> def gen():
... a = 10
... yield a
... a = a*10
... yield a
... yield 1000
...
>>> for i in gen():
... print i
...
10
100
1000生成器表达式:
1
2
3>>> L = [ x**2 for x in range(3)]
>>> L
[0, 1, 4]表推导
1
2
3
4
5
6
7
8
9
10
11
12
13>>> G = ( x**2 for x in range(3) )
>>> G
<generator object <genexpr> at 0x7feb3b6fd2d0>
>>> G.next()
0
>>> G.next()
1
>>> G.next()
4
>>> G.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
动态类型
1 | 1.动态类型 |
常见函数OOP
1 | hasattr(o,f) |
数据库
1 | 1. 连接数据库 |