迭代器:iterhtml
迭代器時訪問集合元素的一種方式。python
/alex3714/articles/5143440.htmlapp
obj_iter.__next__() 只有一個next方法異步
obj_fileopen.read() 將全部文件讀入函數
obj_fileopen.readlines()將全部文件按行讀入spa
for line in obj_fileopen:code
print(line) #按行讀入,不會將全部文件一次性讀入htm
生成器:generatorblog
一個函數調用時返回一個迭代器,那這個函數就叫生成器,若是函數中包含yield,那這個函數就變成了生成器內存
1 >>> def cash_money(amount): 2 ... while amount >0: 3 ... amount -= 100 4 ... yield 100 5 ... print("又來取錢了") 6 ... 7 >>> atm = cash_money(300) 8 >>> atm.__next__() 9 100 10 >>> atm.__next__() 11 又來取錢了 12 100 13 >>> atm.__next__() 14 又來取錢了 15 100 16 >>> atm.__next__() 17 又來取錢了 18 Traceback (most recent call last): 19 File "<stdin>", line 1, in <module> 20 StopIteration
把函數執行一部分,先中斷,能夠暫時去幹別的,再回來繼續作,即實現異步操做。能夠在while循環中切出切入
/wupeiqi/articles/4980620.html
在python中,函數能夠做爲參數,把函數名傳遞給函數做爲形參,亦可return一個函數,將包含一個函數做爲形參的函數返回值賦值給一個變量,那麼這個變量加上()就開始執行這個返回的函數。例子:
1 def login(obj_func): 2 print("This is login page.") 3 print("You had successed login!") 4 return obj_func 5 6 def homepage(): 7 print("This is the homepage.") 8 9 def tv(): 10 print("This is the TV page.") 11 12 def shop(name): 13 print("This is the shop page.") 14 15 16 tv = login(tv) 17 tv() 18 >>> 19 This is login page. 20 You had successed login! 21 This is the TV page.
帶參數:
1 def login(obj_func): 2 print("This is login page.") 3 print("You had successed login!") 4 return obj_func 5 6 def homepage(): 7 print("This is the homepage.") 8 9 def tv(name): 10 print("This is the TV page.Hello %s"%name) 11 12 def shop(name): 13 print("This is the shop page.") 14 15 16 tv = login(tv) 17 tv("SuperMan")
至關於把原有的tv替換掉了
下面展現真·終極形態:
1 def login(obj_func): 2 print("This is login page.") 3 print("You had successed login!") 4 5 @login 6 def homepage(): 7 print("This is the homepage.") 8 9 def tv(name): 10 print("This is the TV page.Hello %s"%name) 11 12 def shop(name): 13 print("This is the shop page.") 14 15 16 #tv = login(tv) 再也不用傳入login()方法,直接使用tv()函數,便會自動包漿! 17 tv("SuperMan")
這種形式被稱之爲裝飾器
@login程序一執行,就會掃描裝飾器,至關於執行了tv = login(tv)
上面的程序均有錯誤,在須要擴展功能的函數上方,每個都要加@login才能夠,正確以下:
1 def login(obj_func): 2 def inner(arg): 3 print("This is login page.") 4 print("You had successed login!") 5 obj_func(arg) #tv() 6 return inner 7 8 9 @login 10 def homepage(): 11 print("This is the homepage.") 12 @login 13 def tv(name): 14 print("This is the TV page.Hello %s" % name) 15 @login 16 def shop(name): 17 print("This is the shop page.") 18 19 #tv = login(tv) 20 tv("SuperMan")
該段代碼的構造流程時:
一、在內存中定義login方法
二、遇到@login時,將login()下面的tv()方法傳入login()方法中做爲形參,遇到def inner(arg):方法後,在內存中定義inner()方法,return inner。
三、遇到tv("實參")後,直接去調用inner("形參")方法,執行了驗證,執行完驗證後,當遇到obj_func()arg後程序調至@login處,並將實參帶入tv()方法,執行結束
1 def login(obj_func): 2 def inner(*args): 3 print("This is login page.") 4 print("You had successed login!") 5 obj_func(*args) #tv() 6 return inner 7 8 9 @login 10 def homepage(): 11 print("This is the homepage.") 12 @login 13 def tv(name,passwd): 14 print("This is the TV page.Hello %s\npassword is %s" % (name,passwd)) 15 #print(type(args)) 16 # 17 # def tv(*args): 18 # print("This is the TV page.Hello %s\npassword is %s" % args) 19 # #print(type(args)) 20 21 @login 22 def shop(name): 23 print("This is the shop page.Hello %s"% name) 24 25 #tv = login(tv) 26 tv('SuperMan','123456') 27 shop("daNiu")
傳參裝飾器:
1 print("111") 2 def login(): 3 print("I'm the login() function") 4 def errorHandle(): 5 print("I'm the errorHandle() function") 6 7 def filter(func1,func2): 8 def outer(args): 9 def wrapper(): 10 func1() 11 args() 12 func2() 13 return wrapper() 14 return outer 15 16 @filter(login,errorHandle) 17 def index(): 18 print("I'm the index() function.I also the main() function")