Pythonのyieldは何ですか?

553 ワード

紹介する
コードのreturnの場所を見つけたり、yieldを使ったりすることがありますが、彼らは同じですか?実は、yieldはreturnと似ているように見えますが、実際には全然違います.
使用
def test():
    print("****start****")
    while 1:
        res = yield 1
        print("res:", res)
t = test() #     yield   ,           ,          t
print(next(t)) # next       ,  start,   1   next(t),  1    ,  res    
print("*"*10) #     * 
print(next(t)) #    res = yield    ,  1   yield   ,  res      ,  None,  while    ,1 yield    

# ****start****
# 1
# **********
# res:None
# 1

出てきたの?