I came across the yield keyword when reading other people's python source code. I finally figured it out through various searches. Here is a summary:
In the usual for...in...loop, in is followed by an array. This array is an iterable object, similar to linked lists, strings, and files. It can be mylist= [1, 2, 3], or mylist = [x*x for x in range(3)]. Its disadvantage is that all data is in memory, and if there is a large amount of data, it will consume a lot of memory. (Recommended learning: Python video tutorial)
The generator can be iterated, but it can only be read once. Because it is generated when it is used. For example, mygenerator = (x*x for x in range(3)), note that () is used here, it is not an array, and the above example is [].
The key to my understanding of the generator's ability to iterate is that it has a next() method. The working principle is to call the next() method repeatedly until an exception is caught. You can use the mygenerator above to test.
The function with yield is no longer an ordinary function, but a generator generator that can be used for iteration. The working principle is the same as above.
yield is a keyword similar to return. When yield is encountered during an iteration, the value after yield is returned. The point is: in the next iteration, execution starts from the code after the yield encountered in the previous iteration.
Brief understanding: yield means return returns a value, and remembers the returned position, and the next iteration will start from this position.
Functions with yield are not only used in for loops, but can also be used as parameters of a function, as long as the parameters of this function allow iteration parameters. For example, the array.extend function, its prototype is array.extend(iterable).
The difference between send(msg) and next() is that send can pass parameters to the yield expression. The parameters passed at this time will be used as the value of the yield expression, and the parameters of yield are the values returned to the caller. . ——In other words, send can forcibly modify the previous yield expression value. For example, there is a yield assignment in the function, a
= yield 5. The first iteration here will return 5, and a has not been assigned a value. In the second iteration, use .send(10), then the value of the expression a is forcibly modified to 10, which was originally None, then a=10
send(msg) and next() both There are return values. Their return value is the value of the expression after yield when the current iteration encounters yield. In fact, it is the parameter after yield in the current iteration.
The first call must be next() or send(None), otherwise an error will be reported. The reason why it is None after send is because there is no previous yield at this time (according to Article 8). It can be considered that next() is equivalent to send(None).
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of What does python yield mean?. For more information, please follow other related articles on the PHP Chinese website!