Python generate send () method (detailed description), python generate

Source: Internet
Author: User

Python generate send () method (detailed description), python generate

I searched for it on the Internet. I thought it was hard to explain it for half a day. I 'd like to 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:

1extra1111extraNone1

Why? Click "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 regarded as an overall expression. The content you send will be used as the value of this expression. You can use anything on the left to receive or not receive it, in short, yield is the one you sent in. This expression is what you send in and then continues to execute. If you encounter yield again, output the expression following yield.

Of course, normally, no constant will be output, and a volume related to the received content will be output, otherwise it will not be sent in vain.

The above python generate send () method (detailed description) is all the content shared by the editor. I hope you can give us a reference and support the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.