A generator
Acquaintance Builder 1.1
What is a generator? This concept is vague, the literature has a different understanding, but the core is basically the same. Builder essentially iterator in python community, most of the time regarded iterators and generators are doing the same concept. Why not the same? Why create a generator? Generators and iterators are also different, the only difference is this: Python tools iterator is to provide you with data already written or by converting to come, (such as file handles, iter ([1,2,3]) . Builder is the tool we need to use their own python code build. the biggest difference will be the case.
1.2 generator are built
There are three ways in python to create a generator:
1. function generator
2. Generator derivations
3. python built-in function or module (1,3 on two fundamentally almost the same, are generated in the form of a function, only 1 is to write a function generator, 3 is provided by python generator function only)
1.3 generator function
We first examine the building generated by generator function.
First, we look at a very simple function:
FUNC DEF (): Print (. 11) return 22 is RET = FUNC () Print (RET) # result: . 11 22 is
The function of the return into the yield, so that it is not func function, but rather a function generator
def func(): print(11) yield 22
We did not write any changes, which is why? Let's see what the function name in parentheses is acquired?
FUNC DEF (): Print (. 11) the yield 22 is RET = FUNC () Print (RET) # Run Results: <Object Generator FUNC AT 0x000001A575163888>
The results and the top run is not the same, why ?? Because of yield function, then this function is a generator function.
When we execute this function. Is no longer perform the function. Instead get the generator object, then the object how to generate value it?
Before we say, the generator is essentially an iterator. How iterator values, how to value generator. So we can directly execute the Next () to perform the following generator
FUNC DEF (): Print ( "111") yield 222 . Gener = FUNC () function does not at this time # Perform ⽽ it is to get to ⽣ generator ret = gener .__ next __ () # function only this time YES will print (ret) # func will be produced and yield data 222 to the ret. Results: 111 222
And my generator function can be written in more yield.
FUNC DEF (): Print ( "111") the yield 222 Print ( "333") the yield 444 Gener = FUNC () RET = Gener .__ Next __ () Print (RET) RET2 = Gener .__ next__ () Print (RET2) RET3 = the Next Gener .__ __ () # last frame a yield perform complete __next again __ () procedure error. Print (RET3) results: 111 222 333 444
When the program is running the last of yield, it continues to run behind the Next () program will be given, corresponding to a yield a next, next exceed the number of yield, it will error, like iterators.
yield and return the difference:
In general the return function is provided only one, his role is to terminate the function, and to the return value of the function's execution.
yield the generator function may be provided in a plurality, and that he does not terminate function, next will yield the corresponding acquisition element generates.
For example:
We look at this demand: the old boy selling steamed buns downstairs boss ordered 10,000 buns Baozi Pu boss is very real, they would all worked out.
def eat(): lst = [] for i in range(1,10000): lst.append('包子'+str(i)) return lst e = eat() print(e)
Doing so is no problem, but we did not because the students so much, eat about 2000, and the rest of 8000, we can only accomplish some space, put aside. If Baozi Pu boss efficiency is high enough, I eat a bun, a bun you do, then it will not take up too much storage space, and perfect.
def eat(): for i in range(1,10000): yield '包子'+str(i) e = eat() for i in range(200): next(e)
This difference between the two:
The first is to do it directly to the buns of all, take up memory.
The second is to eat a produce a very memory-saving, but also preserves previous position.
EAT DEF (): for I in Range (1,10000): the yield 'buns' + STR (I) E = EAT () for I in Range (200 is): Next (E) for I in Range ( 300): next (E) # buns multiple next order number is recorded.
1.4 send method (understand, do not speak)
· Next we come to a new understanding of things, send method
# The Next generation can only get the value of yield, but can not pass a value. DEF Gen (name): Print (F ' {name} READY to EAT ' ) the while . 1 : Food = the yield Print (F ' {name} {Start to EAT Food} ' ) Dog = Gen ( ' Alex ' ) Next ( Dog) the Next (Dog) the Next (Dog) # using send this method is possible. DEF Gen (name): Print (F ' {name} READY to EAT ' ) the while . 1 : Food = yield 222 Print (F ' {name} {Start to EAT Food} ' ) Dog = Gen ( ' Alex ' ) next (Dog) # for the first time must be allowed in the first pointer with a yield next back # and next as the yield value can be obtained RET = dog.send ( ' bone ' ) Print (RET) DEF Gen (name): Print (F ' {name} READY to EAT ' ) the while . 1 : Food = the yield Print (F ' {name} {Start to EAT Food} ' ) Dog = Gen ( ' Alex ' ) Next (Dog) # may also be a value transmitted to the yield dog.send ( ' bone ' ) Dog. Send ( ' dog ' ) dog.send ( ' sausage ' )
send and next () the difference:
Same point:
send and next () so that the generator can yield a corresponding downward once.
Yield value can be obtained is generated.
difference:
The first acquisition yield values can only be used next can not send (can send (None)).
can send to yield a set value is transmitted.
1.4 yield from
To provide a direct each iteration of a data object may be in the python3 be returned as a result the generator
# Comparative from yield and yield DEF FUNC (): LST = [ ' Wei Long ' , ' old popsicles ' , ' Arctic ' , ' sheep with ' ] yield LST G = FUNC () Print (G) Print (Next (G )) # only returns a list DEF FUNC (): LST = [ ' Guardian dragon ' , ' old Popsicle ' , ' Arctic ' , ' cattle and sheep with' ] The yield from LST G = FUNC () Print (G) # of each element of the results of each iteration may he will use this object (list) as iterator is returned. Print (the Next (G)) Print (the Next (G)) Print (the Next (G)) Print (the Next (G)) '' ' yield from [' Guardian Dragon ',' old Popsicle ',' Arctic ',' cow with sheep '] is equivalent to: the yield' Waylung ' the yield' old popsicles ' the yield' Arctic ' the yield' with sheep ' ' ''
There is a small hole, the yield from a list each element returns, so if you write two yield from and does not produce alternating effect
DEF FUNC (): LST1 = [ ' Wei Long ' , ' old popsicles ' , ' Arctic ' , ' sheep with ' ] lst2 = [ ' bread ' , ' Hanamaki ' , ' BEAN ' , ' cake ' ] the yield from LST1 the yield from lst2 G = FUNC () for I in G: Print (I)