Python difference in yield with the role of the for loop, vernacular analytical yield keyword

What yield that?

yield is the Python keywords, yield occurs for constructing generator (generator). Python three devices (decorator, iterators, generators) of one, but the builder of the construction method is not only the use of yield keyword method. There generator expressions of the way.

What is a generator?

Python generator works is similar to the process through security checks. Since the larger flow of people, if all of a sudden have security problems occur (blocked the door: blocking, where not enough: out of memory). So we should at the checkpoint placement of staff, to limit the flow, so that people "intermittent" one by one through. The Python yield keyword in fact, the staff of the checkpoint.

The above is my personal understanding, any questions please forgive me!

Python definition of generator is as follows:
generator is a specific function, allowing a return value, and "pause" execution code is later restored (restored from the suspended state).

Speaking of generators have to say another thing: Collaborative program
coordinated program that can run independent function call, you can pause or suspend, place of departure from the program to continue or start again.

For chestnut!
When the cooperative program pause, we can derive the middle of the return value when calling back to the program you can pass additional or changed parameters. But still it resumes where it left off, and restore all intact state. In fact, the generator is a collaborative program.

In addition, the generator or iterables. There are three common methods: next (), send (), close ()

Why yield (generator)?

Some small partner puzzled, generators and for the role of loop iterations effect is the same, why should I use the generator?
A: The
effect of it is not quite true, but when crossing the huge data sets (iteration), the consumption of resources is not an order of magnitude.
Specifically:
for looping through a long list trillion level, will load all the data of the list into memory, if your memory is very small will overflow, even great memory, this operation is very occupied Resources.
Generator is used, it will be state data (for example: Which position in the list to traverse the like) is saved to memory, to read the required data on each call.
Generator is a memory use more friendly structure. Understanding this point of view, it is to switch to the time-space potential generator.

Example Python code generator

  1. Generator expression:
	my_generator = (i for i in range(10))
	print(my_generator)
	>>> <generator object <genexpr> at 0x7f89afbfc4c0>
	print(my_generator[1])
	>>> TypeError: 'generator' object is not subscriptable
	print(next(my_generator))
	>>> 0
	print(next(my_generator))
	>>> 1
	for ele in my_generator:
    	print(ele)
    >>>
    0
	1
	2
	3
	4
	5
	6
	7
	8
	9
  1. Functional generator:
	def my_gen_func(my_list):
    for i in my_list:
        yield i


	my_generator = [i for i in range(5)]
	for ele in my_gen_func(my_generator):
 	   print(ele)
    >>>
    0
	1
	2
	3
	4

Guess you like

Origin blog.csdn.net/qq_19672707/article/details/88710121