标签:
一.生成器
生成器generator
如果函数中包含yield语法,那这个函数就会变成生成器,这个yield的主要效果,就是可以使函数中断,并保存中断状态,中断后,代码可以继续往下执行,过一段时间还可以再重新调用这个函数,从上次yield的下一句开始执行。
示例:
import time
def consummer(name):
print("%s开始吃包子" % name)
while True:
baozi =yield #yield既可以返回暂停一下,也可以接受send发过来的值,包含yield就是生成器,方法next 暂停到19行
print("包子%s来了,被%s吃了" % (baozi,name))
def producer(name2):
c1 = consummer("jack") #调用函数1
c2 = consummer("tom")
c1.__next__() #consummer函数返回的是迭代器 回到9行
c2.__next__()
print("开始做包子")
for i in range(10):
time.sleep(1) #每一秒钟做2个包子 如果注释这行的话,就会一次全部输出
print("%s做了2个包子" % name2)
c1.send("hi")
c2.send(i) #回到12行
producer("alex") #调用函数2
二.迭代器
迭代器是访问集合元素的一种方式。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。
迭代器只能往前不会后退,迭代器的一大优点是不要求事先准备好整个迭代过程中所有的元素。
迭代器仅仅在迭代到某个元素时才计算该元素,而在这之前或之后,元素可以不存在或者被销毁。
示例:
import time
def consumer(name):
print(‘%s准备吃包子‘%name)
while True:
baozi=yield
print(‘包子[%s]来了,被[%s]吃了‘%(baozi,name))
def producer(name):
c=consumer(‘a1‘)
c2=consumer(‘a2‘)
c.__next__()
c2.__next__()
print("开始准备吃包子啦")
for i in range(10):
time.sleep(1)
print(‘做了2个包子‘)
c.send(i)
c2.send(i)
producer(‘xym‘)
三.装饰器
作用:给已经存在的功能扩展新的功能
传统写法:
def login(func):
def inner(atg):
print("passwed user verification...")
func(atg)
return inner
def home(name):
print("Welcome [%s] to home page" % name)
def tv(name):
print("Welcome [%s] to TV page" %name)
def movie(name):
print("Welcome [%s] to movie page" % name)
tv = login(tv)
tv(123)
print(50*"-+")
home = login(home)
home(456)
print(50*"-+")
movie = login(movie)
movie(999)
----------------------------------------------
输出:
passwed user verification...
Welcome [123] to TV page
--------------------------------------------------
passwed user verification...
Welcome [456] to home page
--------------------------------------------------
passwed user verification...
Welcome [999] to movie page
装饰器写法:
def login(func):
def inner(atg):
print("passwed user verification...")
func(atg)
return inner
@login
def tv(name):
print("Welcome [%s] to TV page" %name)
tv(123)
---------------------------------
输出:
passwed user verification...
Welcome [123] to TV page
实现复杂带多个参数装饰器
def login(func):
def inner(*atg,**arg):
print("passwed user verification...")
func(*atg,**arg)
return inner
@login
def tv(name,passwd=‘‘):
print("Welcome [%s] to TV page" %name)
tv(123,passwd = 321)
--------------------------------------
输出;
passwed user verification...
Welcome [123] to TV page
装饰器返回值
#定义一个装饰器
def login(func):
def inner(*atg,**arg):
print("passwed user verification...")
return func(*atg,**arg)
return inner
@login
def tv(name,passwd=‘‘):
print("Welcome [%s] to TV page" %name)
return 4
t = tv(123,passwd = 321)
print(t)
-------------------------------------
输出:
passwed user verification...
Welcome [123] to TV page
4
定义一个默认装饰器,然后给装饰器传入函数,因为写一个装饰器的成本要高于写一个函数
def Before(request,kargs):
print ‘before‘
def After(request,kargs):
print ‘after‘
def Filter(before_func,after_func):
def outer(main_func):
def wrapper(request,kargs):
before_result = before_func(request,kargs)
if(before_result != None):
return before_result;
main_result = main_func(request,kargs)
if(main_result != None):
return main_result;
after_result = after_func(request,kargs)
if(after_result != None):
return after_result;
return wrapper
return outer
@Filter(Before, After)
def Index(request,kargs):
print ‘index‘
标签:
原文地址:http://www.cnblogs.com/4rain1123/p/5786759.html