

Python生成器的一个坑
source link: https://chenjiehua.me/python/python-generator-trick.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python生成器的一个坑
用Python写迭代器(Iterator)的时候,可能会有人向你推荐生成器(Generator),并列举一堆生成器的好处。不过,今天要来分享一个生成器的坑……
假如我们要来写一个归一化函数,简单看个例子:
正常情况下输出结果非常符合预期:
但……
如果 numbers 传入了一个generator,那结果会变成什么样子呢?我们来试一下:
猜猜结果输出什么:
没错,是空列表!怎么回事呢?是for循环里的生成器坏了,试试直接输出:
输出结果:
没问题呀!
那……
排除生成器本身的问题,那肯定是normalize函数错了。断点看一下:
仔细想想,生成器的原理是什么?调用next() 获取下一个元素的值,直到返回 StopIteration。
这样子就说得通了,sum() 把生成器遍历了一遍,等到for循环的时候,已经没有内容需要遍历了。
结果输出:
改……
这里提供一个简单的解决方法:
至于为啥,留给你思考一下。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK