Although the process of creating a Python iterator is powerful, it is often inconvenient to use. The generator is a simple way to complete the iteration.
Generator essence: iterator
Comes with yeild keyword, call this function to get a generator
Multiple function calls result in multiple generators
Send, yeild, while True loops must be used together
Before using send, initialize the program, stop the program to the yeild position, suspend the program, and then send() can be executed until the next yeild program hangs and returns the value after the next yeild (essentially, understand this sentence. Really understand the use of send)
When yeild returns the value, no matter where yeild is, it can return the value behind it. The value code that may be returned is behind yeild, and return must be at the end of the function body, that is, after the return value code, the function is ended.
Coroutine function: contains yeild and send keywords
Yield in the form of expression: call foo() to get a generator. At this time, the code in the function will not be executed. When next() is called, the function is triggered to execute. When the yield program is encountered, the program hangs, and then g.send( Value), the program starts executing when it is suspended. At this time, the value is first given to yield, and then yield is assigned to x. The program continues to execute until the yield program hangs, and the result after yield is returned to the function. If next(g) is called again at this time, the value of yield is None and then assigned to x, and the program continues to execute. This effect is the same as g.send(None). So, send has two roles:
1. Pass the value to yield, and then yield to x.
2. Trigger the execution of the function until the yield stops, and the value after the yield is returned to the function. The operation at this step is the same as next().
Note: To use send() to transfer values, you must first suspend the program, that is, initialize, so that the value transfer can be completed, otherwise an error will be reported.
PS:
When foo() is called, the function will not be executed, but a generator g is obtained. When the generator calls the next() method, the function foo() will be executed. When the function execution encounters yield, the program hangs and yields The return value is returned, and when next() is called again, the program will continue to execute when the yield hangs until the next yield is encountered. When there is no next yield, that is, there is no return value, it will report a StopIteration exception. This is the function The complete process of operation. Next() must have a return value, otherwise it will throw an exception. That is, next() must get a return value, and this value must also be returned by yield. Without yield, there must be no return value.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.