*args就是一個元組
**dict就是字典ide
[x*x for x in range(0,10)] //列表生成式,這裏是中括號
//結果 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]函數
例子:
In [33]: def fun(n):
...: for i in range(1,n):
...: print(i*2)3d
結果:fun(1000000)
會一直打印,直到結束爲止,這樣佔用大量的系統的資源code
(x*x for x in range(0,10)) //生成器, 這裏是小括號
//結果 <generator object <genexpr> at 0x7f0b072e6140>對象
一個直接返回了表達式的結果列表, 而另外一個是一個對象,該對象包含了對錶達式結果的計算引用, 經過循環能夠直接輸出blog
生成式和生成器的區別:
當表達式的結果數量較少的時候, 使用列表生成式還好, 一旦數量級過大, 那麼列表生成式就會佔用很大的內存,而生成器並非當即把結果寫入內存, 而是保存的一種計算方式, 經過不斷的獲取, 能夠獲取到相應的位置的值,因此佔用的內存僅僅是對計算對象的保存排序
yield能夠輸出多個值 return只能輸出一個值(當函數碰到return就跳出整個函數,不會執行下面的):內存
裝飾器不改變原函數的運行方式和內容,裝飾器就是修飾函數 給函數叫上特定的功能
執行結果:
資源
裝飾器傳參數:input
user_auth=False def auth(auth_type): def test(func): def login(*args,**kwargs): if auth_type == 'qq': global user_auth username='shenjie' passwd='123' if user_auth==False: user=input('please input name: ') pwd=input('please input pwd: ') if user==username and pwd==passwd: print('login is success...') user_auth=True else: print('username or passwd is wroing') if user_auth==True: return func(*args,**kwargs) #執行函數test2() else: print('only qq。。。') return login return test @auth('qq') #這句話 就是 test2=auth(test2) def test2(): print('welcome to test2...') test2()