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
    r += r
    print("A2", r)
    data += 1

    r = yield 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))

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

Related articles: