The process under Python:
1 #Encoding=utf-82 """3 the process----tiny processes4 Yield Generator-----Generate an iterative object such as list, Tuple,dir5 1, a function containing yield, it is an iterative object (list, tuple, etc.)6 end every run to yield and keep the scene7 2, producer, consumer behavior;8 9 3, do not need to execute immediately, only when neededTen """ One AA = [1, 2, 3, 4] - forIinchA: - PrintI the - defTest (): -i =0 -A = 4 + whileI <A: - """ + 0 A 1 at 2 - 3 - """ -x =yieldI -i + = 1 - int =Test () - PrintT#<generator Object Test at 0x0000000002541798> to PrintT.next ()#Next () of the generator + PrintT.next ()#Next () of the generator - PrintT.next ()#Next () of the generator the PrintT.next ()#Next () of the generator * #print T.next () #StopIteration $ Panax Notoginseng PrintType (range (0, 5))#<type ' list ' > - PrintType (xrange (0, 5))#<type ' xrange ' > the + deftest2 (): Ax =yield "First , and return" the Print "First %s"%x +x =yield "Second and return%s"%x - Print "Second%s"%x $x =yield $ PrintX#None, no send - - thet =test2 () - PrintT.next ()Wuyi PrintT.send ("Try again")#use Send () the value of x is the parameter of send, the x is empty if you do not use send the PrintT.send ("The second args") - Wu #1 1 2 3 5 8 - Print "==================" About deftest3 (num): $ ifnum = = 1: - yieldNum -i = 1 -b = 1 A whileI <Num: +x =yieldI the -i = B +I $ the the forIinchTEST3 (13): the PrintI the - in """ the 100 prime numbers after 100000, using yield the """ About defis_p (t_int): the ifT_int > 1: the forIinchXrange (2, T_int): the ift_int%i = =0: + returnFalse - returnTrue the Else:Bayi returnFalse the the defget_primes (): -i = 10000 - whileTrue: the ifis_p (i): the #x = yield I the yieldI theI +=1 -i + = 1 the thet =get_primes () the forIinchXrange (0, 100):94 PrintT.next ()
Python yield (generator)