Why does this code yield a generator?
This question already has an answer here:
Because your yield is nested in your for loop, all your values will be added to the generator expression. Generators are basically equivalent to iterators except, iterators retain the values unless deleted whereas generators generate the values once on the fly. The next method of the generator is implicitly called by the way when used in a for loop rather than a generator.
In addition, you must remember that the return keyword returns one and only on value as does yield. The function returns one generator object with yield that is ready to generate all the supplied values. A standard return statement though returns a value and exits the function so that the other values aren't returned.
链接地址: http://www.djcxy.com/p/1566.html上一篇: 解包,扩展解包和嵌套扩展解包
下一篇: 为什么这个代码产生一个发生器?