装饰器,迭代器,生成器
一、裝飾器
無參裝飾器:
?1、示例1:
1 import time #加載時間模塊 2 def war(name): #定義裝飾器 3 def wari(): 4 start = time.time() #定義開始時間 5 name() #相當于執行下面的函數 6 stop = time.time() #定義結束時間 7 print("run time is %s" %(stop-start)) #給原來函數加入的新功能 8 return wari 9 @war 10 def info(): 11 time.sleep(3) 12 print('we are famly!!') 13 14 info()執行@war的時候就表示運行了war(name)這個函數,然后把下面的函數名賦值給name,就表示info=war(info),現在取到的只是info這個函數的內存地址,在裝飾器上運行name()就相當于執行這個info()函數
示例2:
1 def war(name): 2 def wari(*args,**kwargs): 3 start = time.time() 4 name(*args,**kwargs) 5 stop = time.time() 6 print("run time is %s" %(stop-start)) 7 return wari 8 @war 9 def info(): 10 time.sleep(3) 11 print('we are famly!!') 12 13 @war 14 def auth(name,password): 15 time.sleep(2) 16 print('login success!') 17 18 @war 19 def ero(x): 20 time.sleep(1) 21 print('hello %s' %x) 22 23 info() 24 25 auth('xyy',123) 26 ero('xyp')有參裝飾器:
示例1:
1 def auth1(auth_type): 2 def auth(func): 3 def war(*args,**kwargs): 4 if auth_type == 'file': 5 name = input("username: ") 6 pwd = int(input("password: ")) 7 if name == "xyy" and pwd == 123: 8 print("login successfull!") 9 func(*args,**kwargs) 10 else: 11 print("login error!") 12 elif auth_type == 'sql': 13 print('輸入錯誤') 14 return war 15 return auth 16 17 18 @auth1(auth_type='sql') #write=auth(write) 19 def write(): 20 print('welcome to my home!') 21 write()?
二、迭代器
1、例子1:有下標的類型
例子2:沒下標的類型
1 dic = {'a':1,'b':2,'c':3} 2 i = dic.__iter__() 3 while True: 4 try: 5 print(i.__next__()) 6 except StopIteration: 7 break例子3:文件類型
1 with open('war.txt','r+',encoding='utf8') as f: 2 a = f.__iter__() 3 while True: 4 try: 5 print(a.__next__(),end='') 6 except StopIteration: 7 break?
2、判斷是否是可迭代和是否是迭代器:
1 from collections import Iterable,Iterator #加載模塊 2 s = "xyyp" 3 l = ['a','b','c'] 4 t = (1,2,3,4) 5 d = {"s":1,"a":4} 6 f = open('war.txt') 7 (1)Iterable判斷是否可迭代: 8 print(isinstance(s,Iterable)) -->True 9 print(isinstance(l,Iterable)) -->True 10 print(isinstance(t,Iterable)) -->True 11 print(isinstance(d,Iterable)) -->True 12 print(isinstance(f,Iterable)) -->True 13 (2)Iterator判斷是否是迭代器: 14 print(isinstance(s,Iterator)) -->False 15 print(isinstance(l,Iterator)) -->False print(isinstance(t,Iterator)) -->False 16 print(isinstance(d,Iterator)) -->False 17 print(isinstance(f,Iterator)) -->TruePS:只有文件是可迭代對象并且是迭代器。
小結:
迭代器優點:
1,迭代器提供了一種不依賴于索引的取值方式,這樣可以遍歷那些沒有索引的可迭代對象,比如:字典,集合,文件。
2,迭代器于列表比較,迭代器是惰性計算的,更節省內存空間。
迭代器缺點:
1,無法獲取迭代器的長度,使用不如列表索引取值靈活。
2,一次性,只能往后取值,不能倒著取值
?
三、生成器和協程函數
1、生成器就是一個函數,這個函數內包含有yield這個關鍵字。
2、生成器和和return的區別在于return只能返回一次函數的值就結束了,yield能返回多次值。
3、yield把函數變成了生成器,生成器也就是迭代器。
示例1:
1 def count(x): #定義函數 2 print('start count') 3 while x > 0: #函數內容 4 yield x #返回值 5 x -= 1 6 print('done') 7 8 g = count(5) #執行函數 并把返回值賦給一個變量,最后生成迭代器 9 while True: #while循環迭代器 10 try: 11 print(next(g)) 12 except StopIteration: 13 break 4、協程函數:
示例1:
小結:
send和next()的區別:
1、如果函數內yield是表達式形式(food=yield),那么必須先next觸發函數的執行。
2、二者的共同之處都可以讓函數在上一次暫停的位置繼續運行,不同之處在于send在觸發下一次代碼的執行時,會順便給yield傳一個值。
?
四、面向過程函數編程:
示例:
優點:
1、體系結構更加清晰。
2、簡化程序的復雜度。
缺點:
1、可擴展極其的差,所以面向過程的應用場景是:不需要經常變化的軟件。
?
轉載于:https://www.cnblogs.com/xyp-blog123/p/7028892.html
總結
以上是生活随笔為你收集整理的装饰器,迭代器,生成器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 扫描线三巨头 hdu1928hdu 12
- 下一篇: poj1986 Distance Que