Home>

Just looked it up online,It feels like speaking for a long time is not clear,Write it here.

def generator ():
  while true:
    receive=yield 1
    print ("extra" + str (receive))
g=generator ()
print (next (g))
print (g.send (111))
print (next (g))

Output:

1
extra111
1
extranone
1

Why is this so,Click into send to see a sentence

send:resumes the generator and "sends" a value that becomes the result of the current yield-expression.

That is to sayHere yield 1 is treated as an expression,The content of your send will be used as the value of this expression,Whatever you use on the left to receive or not to receive,In short yield is the thing you send in.This expression becomes the thing after you send in and continues to execute,When yield is encountered again, the expression following yield is output.

Of course, if you usually use it, it will not output a constant.Will output a quantity related to what was received,Otherwise, it was not sent for nothing.

  • Previous iOS development details about the property settings readwrite, readonly, retain, copy, assign, nonatomic
  • Next Introduction to ASP Basics Part 4 (Script Variables, Functions, Procedures, and Conditional Statements)