Generator
The call to the generator function returns an object that is the generator (the type () function can be used to judge the object), and then the next () method can be used to obtain a sequence from the object.
The value passed to yield is returned by the next () method.
The next () method pauses the execution of the function when it encounters yield and returns. The execution of the restore function is called next again. You can print information to illustrate the execution of the generator.
def countdown (n): print '---------1 ' while n > 0: print '---------2 ' yield n print '---------3 ' N-= 1 print '---------4 '
Call the generator and see how it works:
c = Countdown (5) C.next () C.next () C.next ()
Co-process
The general function only processes a single set of input parameters, and the co-process processes the input parameters that are sent to it. The co-process is created by the yield statement.
Syntax such as Python builder