In Python, this side-loop mechanism, called the generator:
Conclusion: The generator is essentially a function, and unlike a function, it produces an object that does not execute code inside the function.
1.1.
List Builder
List Builder: lists are directly generated numbers in memory and can be called at any time
The tuple display type is the generator, which is stored only in memory, but is only generated when called
b = [x for x in range]]c = (x for x in range) print (b, c) print (type (b), type (c))
How to create a generator
1. C = (x for x in range (10)), use () to generate
2. Using the yield () method
Use () to generate
c = (x for x in range) # print (C.next) method in Py2 print (c.__next__ ()) # output 0,__next__ () is a private method and is not recommended with print (Next (c ) # output 1, using the next built-in function to iterate the output, when output to the most one element, will not output data for I in C: # for Call C inside the next to iterate output, save memory space print (i, end= ' \ t ') # 2 3 4 5 6 7 8 9 because the front output is 0, 1, so the pointer is pointing to the next bit, so starting from 2
Create with Yield: If a function definition contains the yield keyword, then the function is no longer a normal function, but a generator
The hardest thing to understand is that generator is not the same as the execution of a function. The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. The function that becomes generator, executes at each call to next (), encounters a yield statement return, and executes again from the yield statement that was last returned.
def fun (): print (' OK ') yield 1 # yield keyword, representing fun () non function, is a generator # at this time as return directly ended the function G = Fun () # At this time the fun () is a generator object, not a function print (g) # <generator Object fun at 0x0000000000afdeb8>a = Next (g) print (' A ', a) # receives the value passed back # Next (g) # OK, when encountering yield1, can be considered as return 1, the end of the function execution, G release, so the subsequent print in execution will error print (Next (g)) # Print () is an iterative object, yield 1 Returns a 1 is captured by print, so the iteration output is OK and 1
Generator's Send () method: You must first enter into the generator before using, and then use Send () to assign the value to the last return
def fun (): print (' Ok1 ') count = yield 1 print (count) yield 2b = Fun () b.send (None) # equivalent to next (b), Start into the function until you encounter yield 1 and return to here, perform the following b.send (' Hello world ') TT = b.send (' Hello World ') # into the position of exit yield 1, will ' Hello World ' Assign a value to count to print, and after yield 2, return the result to print (TT) # Assign the result of yield 2 to TT and print
The application of the generator
can also be implemented in single-threaded scenarios with yield implementations Concurrency Operations the effect
Import timedef Consumer (name): print ("%s ready to eat Buns!"%name) while True: baozi = yield print ("Bun [%s] came, was [ %s] Eat! "% (Baozi,name)) def producer (name): C = Consumer (' A ') c2 = consumer (' B ') c.__next__ () c2.__ next__ () print ("Lao Tzu started to make buns!") For I in range: time.sleep (1) print ("Made 2 buns!") C.send (i) c2.send (i) Producer ("ftl1012")
Using the generator to implement Fibonacci
def fib (max): N, b, a = 0, 0, 1 while n <= Max: # print (b, end= ' \ t ') yield B # is equivalent to print (b), the knot we need The fruit is stored first b, a = A, B+a # Assignment is performed at the same time n + = 1gen = fib (6) print (Next gen) print (Next gen) print (Next (gen)) print ( Next (Gen))
"Learning Reference" http://www.cnblogs.com/alex3714/articles/5765046.html
"Learning Reference" http://www.cnblogs.com/yuanchenqi/articles/5830025.html
Learning to learn---builder in Python 1210