Python Builder's classic program

Source: Internet
Author: User

ImportRandomdefget_data ():"""returns 3 random numbers from 0 to 9"""    returnRandom.sample (Range (10), 3) defconsume ():"""displays the dynamic average of each incoming integer list"""Running_sum=0 Data_items_seen=0 whileTrue:data=yieldData_items_seen+=len (data) Running_sum+=sum (data)Print('The running average is {}'. Format (Running_sum/float (data_items_seen))) defProduce (consumer):"""generate sequence set, passed to consumption function (consumer)"""     whileTrue:data=Get_data ()Print('produced {}'. Format (data) consumer.send (data)yield if __name__=='__main__': Consumer=consume () #启动生成器 Consumer.send (None) producer=Produce (consumer) for_inchRange (10):        Print('Producing ...') Next (producer)

Contrast:yield as a statement or an expression.

ImportRandomdefget_data ():"""returns 3 random numbers from 0 to 9"""    returnRandom.sample (Range (10), 3) defconsume (data):"""displays the dynamic average of each incoming integer list"""Running_sum=0 Data_items_seen=0 whileTrue:data=yieldData"""internal suspend generator, yield as expression instead of statement"""        """when the generator runs, yield returns a value that is the value sent externally by the Send method"""Data_items_seen+=len (data) Running_sum+=sum (data)Print('The running average is {}'. Format (Running_sum/float (data_items_seen))) defProduce (consumer):"""generate sequence set, passed to consumption function (consumer)"""     whileTrue:data=Get_data ()Print('produced {}'. Format (data) consumer.send (data)yield if __name__=='__main__': Consumer=consume (0) consumer.send (None) producer=Produce (consumer) for_inchRange (10):        Print('Producing ...') Next (producer)
View Code

Python Builder's classic program

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.