In Python 3, there are a lot of difference compared to python2 . So in this part, I'll talk about the yield
1、Function contains yield
def h():
print ('To be brave')
yield 5
h()
(1)it's not the result we want
(2) How to understand the priciple of the yield
def h():
print ('Wen Chuan')
yield 5
print ('Fighting!')
c = h()
next(c)
It will connitune working untill yield 5 so the result will be 'Wen Chuan '
Because there arennot any yield after that, so there will be an exception
2.、send(msg) and next()
For next , we cannot give another parameter , so we use next(h) , As for send , basicly c.send(None)= next(c)
So that's how we use ,
3、 About the returned value of yield
4、throw() and close() interrupt Generator
the content below is the understanding of close()
5、My understanding
When you have yield in the function, we have to 把 它实例化 之后进行使用
def factorial(n):
for i in n :
yield (i*i)
num=[1,2,3,4,5]
myni=factorial(num)
print(factorial(num))
print(next(myni))
print(next(myni))
print(next(myni))
print(next(myni))
print(next(myni))