2, the generator function written
DEF FUNC ():
A = 10
the yield 20 is
Gen = FUNC () # is not performed, but a generator to generate
different generator function and a normal function of
an ordinary function name () represents a function of execution
2 executes, generator function name () is not a function, but rather generates a generator
yield and return different
1, retun represents a return, while the function execution terminates, the return code is not executed after
2, yield said return, pause function, the function execution sections
DEF FUNC ():
A = 10
the yield 20
B = 30
the yield 40
Gen = FUNC () # generates a generator
gen .__ next __ () # returns 20 perform a first the yield
gen .__ next __ () # 40 is returned from the first a second yeild started to yield
3, how to get the generator
1, the generator function
generator function name ()
2, generator expression
3, the type of conversion
4, the characteristics of the generator
1, saving memory
2, an inert mechanism (Each time the __next __ () method, a value)
3, a forward value can not be back
5, the value generator
the __next __ () method and the send () method distinction
former value represents a downward
latter value represents a downward addition, may also be passed to a yield value
Note: send () method can not be used The first value, the first value can only be used __next __ ()
final yield can not send ()
6, the benefits of generator
save memory
'' '
# Example of the __next __ () method and the send () method of
DEF FUNC ():
A =. 1
A2 = the yield 2
Print (A2)
C =. 3
C2 = the yield. 4
Print (C2)
the yield. 5
Gen = FUNC () # generated a generator
gen .__ next __ () # first performed to yield
gen.send ( 'Test') # pass parameters to the first yield
gen.send ( 'wahaha') # pass parameters to the second yield
Benefits generator # - Examples saving memory
# 100 to buy a disposable garment, where the need to save, wasteful of memory
in Li1 = []
DEF func2 ():
for I in Range (1,11):
li1.append ( 'clothes S% '% I)
return in Li1
RET = func2 ()
Print (RET) to generate a disposable garment # 100 out into the list of the
print (' ---------- 1 ')
# The best way is to order the next one hundred clothes, but do need one, send one, you do not need a special place to save, save memory
# 1 generator function
DEF func3 ():
for i in Range (1,11):
the yield ( 'clothes S%'% I)
Gen = func3 () # generator generates a
print (gen .__ next __ () ) # The first
print (gen .__ next __ () ) # the second member
for i in gen: # generator essentially iterator loop support for
print (i)
Method # 2 generator expression
# Gen = (i for i in the Range (1, 11))
# Print (Gen) # generate a generator (analogy: the order is to buy clothes)
# Print (the Next Gen .__ __ ()) # The first element
# print (gen .__ next __ ( )) # the second member
# for i in gen: # generator essentially iterator supports for loop
# print (i)
Second, the derivation
to derive the formula (list comprehension, Dictionary derived type, etc.)
1, the list is derived formula: [Results for loop if condition]
2, dictionary derived formula: {key: value for the cycle if conditions}
3, a set of derived formula: {key for cycling conditions if}
Note: no inferential tuple
Third, the expression generator
1, the generator if the expression loop :( Results for conditions)
2, an inert mechanism (analog: not bullets clip can not be the same value, generating 2)
generator is recorded in the memory piece of code, when created, not executed
Generator expressions and list comprehensions difference between analogy:
1, buy 100 eggs, list comprehensions: a one-time buy back the 100 eggs, the needs of local storage
2, generator expressions: buy a hen, We need to give lay eggs