+ 3
Yield is used for a generator.
Eg you want a list that counts to infinity but you don't want store every value since it is impossible. You can simple do
def gen_infin() :
n=0
While true:
yield n
n=n+1
for i in gen_infin() :
#dostuff here
Yield "returns" every iteration step the value n. The thing with yield is that if the function gets called again it starts from the last yield call with the same internal state.
Or you could do
def abc() :
Yield "a"
Yield "b"
Yield "c"
gen = abc()
print (gen.next()) #prints a
print (gen.next()) #prints b
print (gen.next()) #prints c
print (gen.next()) # error