yield has two effects:
- Suspend execution of the current code, and record the current position
- Corresponds to the return, the return value may later
yield itself is the generator ! DEF Genter (): A =. 4 B =. 5 C =. 6 for I in Range (. 5): the yield A Print ( 'HHH' + STR (I)) the yield B Print ( "AAA" + STR (I)) the yield C # Genter contains the yield () is a generator RES = Genter () Print (RES) for I, C in the enumerate (RES): # for statement, which encapsulates the next method. To remove one value generator Print ( 'I', I) IF I>. 1: # test results performed to break break Print (C) Print ( '------------- ------------------- ')
result:
0 i
4
--------------------------------
hhh0
i 1
5
----------- ---------------------
aaa0
i 2