Python-生成器/你不知道的点
Posted 狙击手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-生成器/你不知道的点相关的知识,希望对你有一定的参考价值。
1.什么是生成器
通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。
2.创建生成器方法
方法一
要创建一个生成器,有很多种方法。第一种方法很简单,只要把一个列表生成式的[ ]改成( )
创建L和G的区别仅在于最外层的[ ]和( ),L是一个列表,而G是一个生成器。我们可以直接打印出L的每一个元素,但我们怎么打印出G的每一个元素呢?如果要一个一个打印出来,可以通过next()函数获得生成器的下一个返回值:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-21e608f5bfdbad5f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-4fca259b327b613c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-7a93c486e793da0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-9e762978a5d251ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
生成器保存的是算法,每次调用next(G),就计算出G的下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出StopIteration的异常。当然,这种不断调用next()实在是太变态了,正确的方法是使用for循环,因为生成器也是可迭代对象。所以,我们创建了一个生成器后,基本上永远不会调用next(),而是通过for循环来迭代它,并且不需要关心StopIteration异常。
方法2
generator非常强大。如果推算的算法比较复杂,用类似列表生成式的for循环无法实现的时候,还可以用函数来实现。
比如,著名的斐波拉契数列(Fibonacci),除第一个和第二个数外,任意一个数都可由前两个数相加得到:
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
斐波拉契数列用列表生成式写不出来,但是,用函数把它打印出来却很容易:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-8c025aa8025687ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-fb1af78f142c142a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
仔细观察,可以看出,fib函数实际上是定义了斐波拉契数列的推算规则,可以从第一个元素开始,推算出后续任意的元素,这种逻辑其实非常类似generator。
也就是说,上面的函数和generator仅一步之遥。要把fib函数变成generator,只需要把print(b)改为yield b就可以了:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-51bdde2f8cb61363.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-15191474f209c4bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-2df20da87b047571.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-2ee84bf23d546782.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
但是用for循环调用generator时,发现拿不到generator的return语句的返回值。如果想要拿到返回值,必须捕获StopIteration错误,返回值包含在StopIteration的value中:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-d097965a39ea6c8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-2ee6a57b91eb9fd4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.send
例子:执行到yield时,gen函数作用暂时保存,返回i的值;temp接收下次c.send("python"),send发送过来的值,c.next()等价c.send(None)
使用next函数
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-b9b20ee9cc1f6916.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-843fcef459f3427b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
使用__next__()方法
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-06834f377679e28d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-b7d96136914cf56e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
使用send
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-8decf943b1bca95b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
运行结果:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-27f36b269f0ded8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4.实现多任务
模拟多任务实现方式之一:协程
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-828dbf72bd9c9d1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-b3a086a1f56d40fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
总结
生成器是这样一个函数,它记住上一次返回时在函数体中的位置。对生成器函数的第二次(或第n次)调用跳转至该函数中间,而上次调用的所有局部变量都保持不变。
生成器不仅“记住”了它数据状态;生成器还“记住”了它在流控制构造(在命令式编程中,这种构造不只是数据值)中的位置。
生成器的特点:
1.节约内存
2.迭代到下一次的调用时,所使用的参数都是第一次所保留下的,即是说,在整个所有函数调用的参数都是第一次所调用时保留的,而不是新创建的
5.迭代器
迭代是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。
1.可迭代对象
以直接作用于for循环的数据类型有以下几种:
一类是集合数据类型,如list、tuple、dict、set、str等;
一类是generator,包括生成器和带yield的generator function。
这些可以直接作用于for循环的对象统称为可迭代对象:Iterable。
2.判断是否可以迭代
可以使用isinstance()判断一个对象是否是Iterable对象:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-363ef67bbf8aaf8d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-7bf834878f9b6b64.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
而生成器不但可以作用于for循环,还可以被next()函数不断调用并返回下一个值,直到最后抛出StopIteration错误表示无法继续返回下一个值了。
3.迭代器
可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-89a923f96b78c87a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-ca9fac620e5fa514.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4.iter()函数
生成器都是Iterator对象,但list、dict、str虽然是Iterable,却不是Iterator。
把list、dict、str等Iterable变成Iterator可以使用iter()函数:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-62184c4e34713cc3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-5bea573191ebf632.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
总结
·凡是可作用于for循环的对象都是Iterable类型;
·凡是可作用于next()函数的对象都是Iterator类型
·集合数据类型如list、dict、str等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。
·目的是在使用集合的时候,减少占用的内容。
6.闭包
1.函数引用
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-78a7cc9f2df4fd21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-05cc39915cac5b82.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-ff44190bf11c3a88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.什么是闭包
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-39aac80a91d8801f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-98a832b088453726.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-5d96c2df301dedd8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.看一个闭包的实际例子:
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-32d08e344c5fc84d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/6078268-b2615c9bd8d0deb9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这个例子中,函数line与变量a,b构成闭包。在创建闭包的时候,我们通过line_conf的参数a,b说明了这两个变量的取值,这样,我们就确定了函数的最终形式(y = x + 1和y = 4x + 5)。我们只需要变换参数a,b,就可以获得不同的直线表达函数。由此,我们可以看到,闭包也具有提高代码可复用性的作用。
如果没有闭包,我们需要每次创建直线函数的时候同时说明a,b,x。这样,我们就需要更多的参数传递,也减少了代码的可移植性
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群
626062078,我们一起学Python!
以上是关于Python-生成器/你不知道的点的主要内容,如果未能解决你的问题,请参考以下文章