1.生成器的两种定义方式
1 2 3 4 5
| 1. (x*2 for x in range(10))
2.def foo(): yield 1 c=foo()
|
列表生成器
1 2 3 4 5 6 7
| a=[x for x in range(10)] print(a)
def f(n): return n**3 s=[f(c) for c in range(3)] print(s)
|
生成器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| def foo(): print('ok1') yield 1 print('0k2') yield 2 c=foo()
def foo(): print('ok1') yield 1 print('0k2') yield 2
|
2.生成器的两种方法:
next调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| def foo(): print('ok1') yield 1 print('0k2') yield 2 c=foo()
a=next(c) b=next(c)
注:def foo(): print('ok1') yield 1 print('0k2') yield 2 a=next(foo()) b=next(foo()) print(a,b)
|
send调用
1 2 3 4 5 6 7 8 9 10 11 12 13
| def foo(): print('ok1') a=yield 1 ① print(a) print('0k2') b=yield 2 print(b) c=yield 3 print(c) c=foo() c.send(None) c.send('abc') c.send('cba')
|
注:生成器在定义时值的数量是固定的,调用超出会StopIteration
yield 实现伪并发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import time def cons(name): print("%s准备吃包子了" % name) while True: baozi = yield print("包子%d来了 %s开始吃包子了" % (baozi,name)) def do(name): print("准备做包子了") a=cons('a') b=cons('b') a.__next__() b.__next__() print('开始做包子了') for i in range (1,5): time.sleep(2) print('做了2个包子') a.send(i) b.send(i) do('chen')
|
3.可迭代类型 iterable
3.可迭代类型 iterable
- 列表 list
- 元组 tuple
- 集合 set
- 字典 dict
1 2 3 4 5 6 7 8
| a=[1,2,3] b=(1,2,3) c={1,2,3} d={'name':''} print(type(a)) print(type(b)) print(type(c)) print(type(d))
|
4.迭代器 iterator ——遵循迭代器协议
(1.)next方法
(2.)iter()方法
1 2 3 4 5 6 7 8
| a=[1,2,3] b=(1,2,3) c={1,2,3} d={'name':''} print(iter(a)) print(iter(b)) print(iter(c)) print(iter(d))
|
4.for循环作用
1 2 3 4 5
| for i in iterable print i
|
5.python装饰器
装饰器的实现条件:
1.函数的定义域
2.高阶函数的使用
闭包:
1 2 3 4 5 6
| def outer(): x = 10 def inner(): print(x) return inner outer()()
|
装饰器
1 2 3 4 5 6 7 8 9 10 11
| import time def show_time(f):
def inner(): start=time.time() f() end=time.time() print("time=%d",(end-start)) return inner def foo(): print("foo....")
|
第一种调用方式:
1 2
| foo=show_time(foo) foo()
|
第二种调用方式:
1 2
| @show_time #foo=show_time(foo) foo()
|
装饰器函数的参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import time def logger(flag=''): def show_time(f): def inner(*x,**y): start=time.time() f(*x,**y) end=time.time() print("time=%d",(end-start)) if (flag=='ture'): print("日志记录") return inner return show_time @logger('') #调用装饰器函数传入参数 判断是否打印日志文件 def foo(): print("foo0......") time.sleep(3) foo() @logger('ture') def add(*a,**b): sum=0 for i in a: sum+=i print(sum) time.sleep(1) add(1,2,3,4,5,7,8)
|
简单实现登录系统功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| sername,password='chen','123' wxname,wxpasswd='wang','567' option = False def choseup(a=''): def up(f): def inner(): if option == False : if a=='jd': user1=input("username=\n") passwd1=input("password=\n") if user1==username and passwd1==password : print("登录成功") f() option == True else: print("账号密码错误请重新登录") inner() elif a=='wx': user2=input("username=\n") passwd2=input("password=\n") if user2==wxname and passwd2==wxpasswd: print("登录成功") f() option == True else: print("账号密码错误请重新登录") inner() else: pass return inner return up
@choseup('jd') #传输参数‘jd’,选择登录方式为jd def sort_page1(): print("welcome to home") @choseup('wx') #传输参数‘wx’,选择登录方式为wx def sort_page2(): print("welcome to finale") @choseup('jd') def sort_page3(): print("welcome to book")
def a(): x = input("1.home\n2.finale\n3.book\n")
print('您的选择为:',x) if (x == '1'): sort_page1() elif (x == '2'): sort_page2() elif (x == '3'): sort_page3() a()
|
Author:
FullSky
Permalink:
http://FullSky`★.com/2019/08/27/python迭代器、生成器、装饰器/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
能通途偶遇在这星球上,我多么够运。