迭代器、生成器、装饰器

迭代器: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")
相关文章
相关标签/搜索