__author__ = ' zhht ' #!/usr/bin/env python#-*-coding:utf-8-*-#返回当前执行到的函数的返回值. and maintains the status of the current execution. It's time to do something else. The next time you execute, the location where the last call was received continues to execute the code. def fib (num): n = 0 a,b=0,1 while n <num: yield b a,b=b,a+b n+=1 return ' complete ' F = fib (5) While True: try: x = Next (f) print ("value%d"%x) except Stopiteration as E: print ("Generator return value-'%s %e.value) break# The second is an example: Def gen (): i = 0 while i < 5: temp = yield i print (temp) i+= 1f = Gen () print (Next (f)) print (Next (f)) print (next) #它和上面的相同点就是能够继续迭代执行. The point is that the process goes to yield returns the value of I, and at the end of the execution, temp receives none, while send sends a value of your temp acceptance in such a case print (F.send ("HAHA1")) Print (F.send (" HAHA2 "))
The python generator has simple code to understand.