Python_day4:生成器、迭代器、装饰器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python_day4:生成器、迭代器、装饰器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4467字,纯文字阅读大概需要7分钟。
内容图文
![Python_day4:生成器、迭代器、装饰器](/upload/InfoBanner/zyjiaocheng/856/0869a497dc8547bb9d792fbbb3e4cf59.jpg)
一、生成器
1、列表生成式
>>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> a = map(lambda x:x+1, a) >>> a <map object at 0x101d2c630> #生成对象 >>> for i in a:print(i) #循环打印
>>> a = [i+1 for i in range(10)] >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]#列表生成
2、生成器
通过列表生成式可以直接生成一个列表,但受内存限制列表容量有限,如果数据过大,访问不到最后的数据,只会造成浪费。为了节省空间,在Python中,有种一边循环一边计算的机制,称为生成器:generator。
要创建一个generator,一种简单方法就是把一个列表生成式的[]
改成()
,就创建了一个generator:
![Python_day4:生成器、迭代器、装饰器 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506064618775.jpg)
![Python_day4:生成器、迭代器、装饰器 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506064618915.jpg)
1 >>> L = [x * x for x in range(10)] #这是列表[] 2 >>> L 3 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 4 >>> g = (x * x for x in range(10)) #这是生成器() 5 >>> g 6 <generator object <genexpr> at 0x1022ef630> #对象 7 8 >>> next(g) #打印数据用__next__()方法 9 0 10 11 #当next()数据打印完之后没有数据会出现如下错误 12 Traceback (most recent call last): 13 File "<stdin>", line 1, in <module> 14 StopIteration 15 16 #正确调用方式用for循环 17 >>> g = (x * x for x in range(10)) 18 >>> for n in g: 19 ... print(n)生成器
通过yield实现在单线程的情况下实现并发运算
![Python_day4:生成器、迭代器、装饰器 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506064618775.jpg)
![Python_day4:生成器、迭代器、装饰器 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506064618915.jpg)
1 import time 2 def consumer(name): 3 print("%s 吃包子!" %name) 4 while True: 5 baozi = yield #每次调用__next__()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行,在循环过程中不断调用yield,就会不断中断 6 print("包子[%s]来了,被[%s]吃了!" %(baozi,name)) 7 8 def producer(name): 9 c = consumer('A') 10 c2 = consumer('B') 11 c.__next__() 12 c2.__next__() 13 print("做包子!") 14 for i in range(10): 15 time.sleep(1) 16 print("做了个包子!") 17 c.send(i) 18 c2.send(i) 19 20 producer("A")yield并发
二、迭代器
首先我们应该知道可用于for循环的数据类型有哪些:
1、集合数据类型,如list
、tuple
、dict
、set
、str
等;
2、generator
,包括生成器和带yield
的generator function。
可以直接作用于for循环的对象成为可迭代对象:Iterable
1 #判断是否为iterable用isinstance 2 >>> from collections import Iterable 3 >>> isinstance([], Iterable) 4 True
可以被__next__()
函数调用并不断返回下一个值的对象称为迭代器:Iterator
。
#判断是否为iterator如下 >>> from collections import Iterator >>> isinstance((x for x in range(10)), Iterator) True >>> isinstance([], Iterator) False
生成器都是Iterator
对象,但list
、dict
、str
虽然是Iterable
,却不是Iterator
。
三、装饰器
定义:本质是函数,为其他函数作修饰
原则:1.不能修改被修饰函数的源代码;2.不能修改被装饰函数的调用方式。
对此,我们应该了解三个概念:
1.函数即变量;2.高阶函数;3.嵌套函数。 高阶函数+嵌套函数=装饰器
1、函数调用顺序:在python中,不允许函数在未声明之前引用或调用。
1 def bar(): 2 print ('in the bar') 3 def foo(): 4 print( 'in the foo') 5 bar() 6 foo() 7 8 def foo(): 9 print ('in the foo') 10 bar() 11 def bar(): 12 print ('in the bar') 13 foo() #只要在调用之前应声明bar()就好,与谁先声明无关
2、高阶函数:a.把一个函数名当做实参传给另外一个函数
b.返回值中包含函数名
1 #把一个函数名当做实参传给另一个函数:在不修改源代码下,添加附加功能 2 import time 3 def bar(): 4 time.sleep(1) 5 print("in the bar") 6 def foo(func): 7 start_time = time.time() 8 func() #run bar 9 stop_time = time.time() 10 print("the func run time is %s" %(stop_time-start_time)) 11 foo(bar)
1 #返回值中包含函数名,不改变调用方式 2 def foo2(func): 3 print(func) 4 return func 5 bar = foo2(bar) #把之前的bar覆盖 6 bar() #run bar
3、嵌套函数
1 def foo(): 2 print("in the foo") 3 def bar(): #相当于定义一个局部变量 4 print("in the bar") 5 bar() #内部调用 6 foo()
4、装饰器(无参数)
1 import time 2 def timer(func): #timer(test1)返回deco内存地址 func=test1 3 def deco(): 4 start_time = time.time() 5 func() #run test1() 6 stop_time = time.time() 7 print("the func run time is %s" %(stop_time-start_time)) 8 return deco 9 10 @timer #语法糖,相当于test1 = timer(test1) 11 def test1(): 12 time.sleep(1) 13 print("in the text1") 14 #test1 = timer(test1) 15 test1()
5、装饰器(有参数)
1 1 import time 2 2 def timer(func): #timer(test1)返回deco内存地址 func=test1 3 3 def deco(*args,**kwargs): 4 4 start_time = time.time() 5 5 func(*args,**kwargs) #run test1() 6 6 stop_time = time.time() 7 7 print("the func run time is %s" %(stop_time-start_time)) 8 8 return deco 9 9 10 10 @timer #语法糖,相当于test1 = timer(test1) 11 11 def test1(): 12 12 time.sleep(1) 13 13 print("in the text1") 14 14 #test1 = timer(test1) 15 15 test1()
四、常用模块THIS
内容总结
以上是互联网集市为您收集整理的Python_day4:生成器、迭代器、装饰器全部内容,希望文章能够帮你解决Python_day4:生成器、迭代器、装饰器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。