Understanding of Python in the yield of

Builder: function generator yield expression is constituted; each generator is a iterator (iterator but not necessarily the generator). return iterators;

 

yieldFunctions similarly return, except that it returns 生成器.

What is a builder, you can pop that in a function using a function of yield instead of the return position, is the generator.

It differs from the method used functions are: to use the return function return values, Each call returns a new processed data back to you; the yield different, when it calls the generator, the data generator Object, then when you need to use, and use the next () method to take, at the same time irreversible.

def test():
   for i in range(1,10):
yield i        #装入

ob = test () #generator type
print next (ob) # 1 is loaded first data release (first in first out)
Print Next (OB) # 2
Print Next (OB). 3 #
Print Next (OB) # 4

 

return: return may terminate execution of the function, the function may return processed data, but requires the data into memory; return later after no longer execute;

 yield: all data will not be taken out into memory; but returns an object; you can get the data through an object; with how much to take, you can save content space. In addition to a return value, the cycle will not terminate the operation;

Guess you like

Origin www.cnblogs.com/12james/p/11797824.html