- 生成器不保留迭代過后的結(jié)果
仔細(xì)看拴魄,發(fā)現(xiàn)(i for i in range(5))生成的是迭代器不是tuple喲佛寿。
In [12]: gen = (i for i in range(5))
In [13]: 2 in gen
Out[13]: True
In [14]: 3 in gen
Out[14]: True
In [15]: 1 in gen
Out[15]: False # 1為什么不在gen里面了? 因?yàn)檎{(diào)用1->2,這個(gè)時(shí)候1已經(jīng)不在迭代器里面了,被按需生成過了
In [20]: gen = (i for i in range(5))
In [21]: a_list = list(gen) # 可以轉(zhuǎn)化成列表诗力,當(dāng)然a_tuple = tuple(gen) 也可以
In [22]: 2 in a_list
Out[22]: True
In [24]: 1 in a_list # 就算循環(huán)過,值還在
Out[24]: True
- lambda在閉包中會(huì)保存局部變量
my_list = [lambda: i for i in range(5)]
for l in my_list:
print l()
4
...
4
其實(shí)就是當(dāng)我賦值給my_list的時(shí)候郁竟,lambda表達(dá)式就執(zhí)行了i會(huì)循環(huán)蚂四,直到 i =4,i會(huì)保留
但是可以用生成器蒿柳,代碼如下:
my_gen = (lambda: n for n in range(5))
for l in my_gen:
print(l())
0
1
2
3
4
也可以堅(jiān)持用list舅列,代碼如下:
my_list = [lambda x=i: x for i in range(5)] # 看我給每個(gè)lambda表達(dá)式賦了默認(rèn)值
for l in my_list:
print(l())
0
1
2
3
4
-重用全局變量
def foo():
# 函數(shù)作用域里定義了一個(gè)本地變量var(var = "local"),而這里是定義之前就使用了var,python解釋器在這里會(huì)認(rèn)為沒有定義本地變量
# 注意這種情況下去調(diào)用print var時(shí),不會(huì)把var看做全局變量了声滥,因?yàn)閜ython解釋器是先查局部變量眉撵,找不到才使用全局變量,而這里局部變量是有的
print var
var = "local"
var = "global"
foo()
------------------------------------------------
UnboundLocalErrorTraceback (most recent call last)
<ipython-input-46-624891b0d01a> in <module>()
----> 1 foo()
<ipython-input-43-373dd100efad> in foo()
1 def foo():
----> 2 print var
3 var = 32
4
UnboundLocalError: local variable 'var' referenced before assignment
函數(shù)作用域里定義了一個(gè)本地變量var(var = "local"),而這里是定義之前就使用了var,python解釋器在這里會(huì)認(rèn)為沒有定義本地變量
注意這種情況下去調(diào)用print var時(shí),不會(huì)把var看做全局變量了纽疟,因?yàn)閜ython解釋器是先查局部變量罐韩,找不到才使用全局變量,而這里局部變量是有的
其實(shí)污朽,可以在函數(shù)里用global關(guān)鍵字注明使用的是全局變量伴逸,不過這樣一來函數(shù)內(nèi)部就改變了全局變量了
def foo():
global var # 聲明了是全局變量
print var
var = "local"
var = "global"
foo()
# output:
global
local
- 可變對(duì)象的*其實(shí)是引用傳遞
In [72]: my_list1 = [[1, 2, 3]] * 2
In [73]: my_list1
Out[73]: [[1, 2, 3], [1, 2, 3]]
In [74]: my_list1[1][0] = 'a' # 我只修改子列表中的一項(xiàng)
In [75]: my_list1
Out[75]: [['a', 2, 3], ['a', 2, 3]] # 但是都影響到了
In [76]: my_list2 = [[1, 2, 3] for i in range(2)] # 用這種循環(huán)生成不同對(duì)象的方法就不影響了
In [77]: my_list2[1][0] = 'a'
In [78]: my_list2
Out[78]: [[1, 2, 3], ['a', 2, 3]]
- datetime也有布爾值
In [34]: import datetime
In [35]: print('"datetime.time(0,0,0)" (Midnight) ->', bool(datetime.time(0,0,0)))
('"datetime.time(0,0,0)" (Midnight) ->', False)
In [36]: print('"datetime.time(1,0,0)" (1 am) ->', bool(datetime.time(1,0,0)))
('"datetime.time(1,0,0)" (1 am) ->', True)
- bool其實(shí)是int的子類
In [97]: isinstance(True, int)
Out[97]: True
In [98]: True + True
Out[98]: 2
In [99]: 3 * True + True
Out[99]: 4
In [100]: 3 * True - False
Out[100]: 3
In [104]: True << 10
Out[104]: 1024
dic = {1:"1"}
dic[True] = "True"
dic
# output:
{1: 'True'}
異常處理加else
In [150]: try:
.....: print('third element:', a_list[2])
.....: except IndexError:
.....: print('raised IndexError')
.....: else:
.....: print('no error in try-block') # 只有在try里面沒有異常的時(shí)候才會(huì)執(zhí)行else里面的表達(dá)式
.....:
raised IndexError # 拋異常了 沒完全完成
In [153]: i = 0
In [154]: while i < 2:
.....: print(i)
.....: i += 1
.....: else:
.....: print('in else')
.....:
0
1
in else # while也支持哦~
In [155]: i = 0
In [156]: while i < 2:
.....: print(i)
.....: i += 1
.....: break
.....: else:
.....: print('completed while-loop')
.....:
0 # 被break了 沒有完全執(zhí)行完 就不執(zhí)行else里面的了
In [158]: for i in range(2):
.....: print(i)
.....: else:
.....: print('completed for-loop')
.....:
0
1
completed for-loop
In [159]: for i in range(2):
.....: print(i)
.....: break
.....: else:
.....: print('completed for-loop')
.....:
0 # 也是因?yàn)閎reak了