On the preliminary understanding of yield
- 2020-06-03 07:03:30
- OfStack
As follows:
def go():
while True:
data = 1
r = yield data # data Is the return value, r Is to receive value
print("data", data)
print("A1", r)
data += 1
r = yield data
print("data",data)
r += r
print("A2", r)
data += 1
r = yield data
print("data",data)
print("A3", r)
# The runtime can't find the bottom after that 1 a yield , will report an error StopIteration
my = go()
print("my", my)
print("None", my.send(None))
print(my.send("1"))
print(my.send("2"))
print(my.send("3"))
my.send(None): This step returns the value of data after the first yield.
send1, run the code between two yield, and finally return the data value after the last yield. If the last yield statement is missing, an error "Stoplteration" will be reported.
(3) r = yield data
r is the incoming data of my.send (' incoming data ')
data returns data after running this segment