本节大纲
迭代器
迭代器是访问集合元素的一种方式。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退。另外,迭代器的一大优点是不要求事先准备好整个迭代过程中所有的元素。迭代器仅仅在迭代到某个元素时才计算该元素,而在这之前或之后,元素可以不存在或者被销毁。这个特点使得它特别适合用于遍历一些巨大的或是无限的集合,比如几个G的文件
特点:
生成一个迭代器:
1 __author__ = ‘Administrator‘ 2 # *-* coding:utf-8 -*- 3 #生成一个列表 4 name = [‘abc‘,‘jack‘,‘list‘] 5 #生成一个迭代器 6 name = iter([‘abc‘,‘jack‘,‘list‘]) 7 print(name,type(name)) 8 #取第一次 9 print("取第一次",name.__next__()) 10 #取第二次 11 print("取第二次",name.__next__()) 12 #取第三次 13 print("取第三次",name.__next__()) 14 #取第四次 15 print("取第四次",name.__next__()) 16 ----------------------------------------------- 17 输出: 18 <list_iterator object at 0x000000000117EF60> <class ‘list_iterator‘> 19 取第一次 abc 20 取第二次 jack 21 取第三次 list 22 Traceback (most recent call last): 23 File "F:/python/day4/迭代器.py", line 16, in <module> 24 print("取第四次",name.__next__()) 25 StopIteration
生成器generator
定义:一个函数调用时返回一个迭代器,那这个函数就叫做生成器(generator),如果函数中包含yield语法,那这个函数就会变成生成器
1 def cash_out(amount): 2 while amount >0: 3 amount -= 1 4 yield 1<br> print("擦,又来取钱了。。。败家子!") 5 6 7 8 ATM = cash_out(5) 9 10 print("取到钱 %s 万" % ATM.__next__()) 11 print("花掉花掉!") 12 print("取到钱 %s 万" % ATM.__next__()) 13 print("取到钱 %s 万" % ATM.__next__()) 14 print("花掉花掉!") 15 print("取到钱 %s 万" % ATM.__next__()) 16 print("取到钱 %s 万" % ATM.__next__()) 17 print("取到钱 %s 万" % ATM.__next__()) #到这时钱就取没了,再取就报错了 18 print("取到钱 %s 万" % ATM.__next__())
作用:
这个yield的主要效果呢,就是可以使函数中断,并保存中断状态,中断后,代码可以继续往下执行,过一段时间还可以再重新调用这个函数,从上次yield的下一句开始执行。
另外,还可通过yield实现在单线程的情况下实现并发运算的效果
1 __author__ = ‘Administrator‘ 2 # -*- coding:utf-8 -*- 3 ‘‘‘ 4 #生成一个列表 5 name = [‘abc‘,‘jack‘,‘list‘] 6 #生成一个迭代器 7 name = iter([‘abc‘,‘jack‘,‘list‘]) 8 print(name,type(name)) 9 #取第一次 10 print("取第一次",name.__next__()) 11 #取第二次 12 print("取第二次",name.__next__()) 13 #取第三次 14 print("取第三次",name.__next__()) 15 16 #取第四次 17 print("取第四次",name.__next__()) 18 ‘‘‘ 19 ‘‘‘ 20 def cash_out(amount): 21 while amount >0: 22 amount -= 1 23 yield 1 24 print("擦,又来取钱了。。。败家子!") 25 26 ATM = cash_out(5) 27 28 print("取到钱 %s 万" % ATM.__next__()) 29 print("花掉花掉!") 30 print("取到钱 %s 万" % ATM.__next__()) 31 print("取到钱 %s 万" % ATM.__next__()) 32 print("花掉花掉!") 33 print("取到钱 %s 万" % ATM.__next__()) 34 print("取到钱 %s 万" % ATM.__next__()) 35 print("取到钱 %s 万" % ATM.__next__()) #到这时钱就取没了,再取就报错了 36 print("取到钱 %s 万" % ATM.__next__()) 37 ‘‘‘ 38 ‘‘‘ 39 def cash_mony(amount): 40 while amount >0: 41 amount -=100 42 yield 100 43 print("又来取钱了...") 44 atm = cash_mony(500) 45 # print(type(atm)) 46 print(atm.__next__()) 47 print(atm.__next__()) 48 print(atm.__next__()) 49 print(atm.__next__()) 50 print(atm.__next__()) 51 52 ‘‘‘ 53 import time 54 def consumer(name): 55 print("%s 准备吃包子啦!" %name) 56 while True: 57 baozi = yield 58 59 print("包子[%s]来了,被[%s]吃了!" %(baozi,name)) 60 61 def producer(name): 62 c = consumer(‘A‘) 63 c2 = consumer(‘B‘) 64 c.__next__() 65 c2.__next__() 66 print("老子开始准备做包子啦!") 67 for i in range(10): 68 time.sleep(1) 69 print("做了2个包子!") 70 c.send(i) 71 c2.send(i) 72 73 #producer("alex")
装饰器
作用:给已经存在的功能扩展新的功能
传统写法:
1 def login(func): 2 def inner(atg): 3 print("passwed user verification...") 4 func(atg) 5 return inner 6 7 8 def home(name): 9 print("Welcome [%s] to home page" % name) 10 def tv(name): 11 print("Welcome [%s] to TV page" %name) 12 13 def movie(name): 14 print("Welcome [%s] to movie page" % name) 15 tv = login(tv) 16 tv(123) 17 print(50*"-+") 18 home = login(home) 19 home(456) 20 print(50*"-+") 21 movie = login(movie) 22 movie(999) 23 24 25 ---------------------------------------------- 26 输出: 27 passwed user verification... 28 Welcome [123] to TV page 29 -------------------------------------------------- 30 passwed user verification... 31 Welcome [456] to home page 32 -------------------------------------------------- 33 passwed user verification... 34 Welcome [999] to movie page
装饰器写法:
1 __author__ = ‘Administrator‘ 2 #-*- coding:utf-8 -*- 3 def login(func): 4 def inner(atg): 5 print("passwed user verification...") 6 func(atg) 7 return inner 8 9 @login 10 def tv(name): 11 print("Welcome [%s] to TV page" %name) 12 tv(123) 13 14 --------------------------------- 15 输出: 16 passwed user verification... 17 Welcome [123] to TV page
实现复杂带多个参数装饰器
1 def login(func): 2 def inner(*atg,**arg): 3 print("passwed user verification...") 4 func(*atg,**arg) 5 return inner 6 7 #装饰器,也可以称之为语法堂 8 #@装饰符 程序已执行就会扫描装饰器并且执行 9 @login 10 def tv(name,passwd=‘‘): 11 print("Welcome [%s] to TV page" %name) 12 tv(123,passwd = 321) 13 -------------------------------------- 14 输出; 15 passwed user verification... 16 Welcome [123] to TV page
装饰器返回值
1 __author__ = ‘Administrator‘ 2 #-*- coding:utf-8 -*- 3 #定义一个装饰器 4 def login(func): 5 def inner(*atg,**arg): 6 print("passwed user verification...") 7 return func(*atg,**arg) 8 return inner 9 10 #装饰器,也可以称之为语法堂 11 #@装饰符 程序已执行就会扫描装饰器并且执行 12 @login 13 def tv(name,passwd=‘‘): 14 print("Welcome [%s] to TV page" %name) 15 return 4 16 t = tv(123,passwd = 321) 17 print(t) 18 ------------------------------------- 19 输出: 20 passwed user verification... 21 Welcome [123] to TV page 22 4
定义一个默认装饰器,然后给装饰器传入函数,因为写一个装饰器的成本要高于写一个函数
#!/usr/bin/env python #coding:utf-8 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‘
递归
1 def digui(coun): 2 print(coun) 3 if coun/2 > 1: 4 res = digui(coun/2) 5 print("res:",res) 6 print("None11111:",coun) 7 return coun 8 digui(10) 9 ------------------------ 10 输出: 11 #递归运算过程 12 10 13 5.0 14 2.5 15 @递归运算最终结果 16 1.25 17 #递归运算退出过程,运算过程中就如多少层,退出的时候就会退出多少层, 18 None11111: 1.25 19 res: 1.25 20 None11111: 2.5 21 res: 2.5 22 None11111: 5.0 23 res: 5.0 24 None11111: 10
利用函数编写如下数列:
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
1 __author__ = ‘Administrator‘ 2 #-*- coding:utf-8 -*- 3 def func(arg1,arg2,stop): 4 if arg1 == 0: 5 print(arg1, arg2) 6 arg3 = arg1 + arg2 7 print(arg3) 8 if arg3 < stop: 9 func(arg2,arg3,stop) 10 print("退出过程:",arg3) 11 12 func(0,1,30) 13 --------------------------------- 14 输出: 15 0 1 16 1 17 2 18 3 19 5 20 8 21 13 22 21 23 34 24 退出过程: 21 25 退出过程: 13 26 退出过程: 8 27 退出过程: 5 28 退出过程: 3 29 退出过程: 2 30 退出过程: 1
python-Day4-迭代器-yield异步处理--装饰器
原文:http://www.cnblogs.com/nb-blog/p/5196925.html