generator function : a function containing yield statements;
Builder Object : The Builder object behaves similarly to the iterator object and supports an iterative interface: __next__ (), which requires an iterative protocol ' if you want to execute the internal statement of the generator function.
a, when the generator function is called , does not execute the function inner statement, but returns a generator object;
B, each execution of a statement, returns a corresponding yield value, although the function is a yield temporarily returned value, but retains the state of the program run, that is, when executing g.__next__ (), the execution of the function statement, and return the second yield corresponding value When all yield return values are executed, the program throws a stop iteration exception: stopiterstion; This behavior, like iterators, implements an interface for iterators.
deff ():Print('in F (), 1') yield1Print('in F (), 2') yield2Print('in F (), 3') yield3g=f ()Print(g)#output: <generator object F at 0x056f8210>Print(g.__next__())#output: In F (), 11Print(g.__next__())#output: In F (), 22Print(g.__next__())#output: In F (), 33Print(g.__next__())#Output: Stopiteration
C, the Generator object is also an iterative object, that is, it can be placed in the for loop after the in, then the generator object implements the __iter__ () method, returns itself;
for inch g: Print (x)
# output: In F (), 1 1 in f (), 2 2 in F (), 3 3
D, the Generator object implements the iterator interface (__next__ ()), and implements an iterative interface (__ITER__ ()), which returns itself;
Print (g.__iter__ is g)
# Output True
Python: Generator function