Python Advanced Features-builder

Source: Internet
Author: User

A mechanism that loops one side of a loop in Python as a generator (generator)

Executes at each invocation next() , yield encounters a statement return, and resumes execution from the last statement returned yield .

Generate list

 for  in range ()>>>1, 4, 9, 16, 25, 36, 49, 64, 81]

Generate generator

 for  in range (+)>>> G<generator object <genexpr> at 0x7f5cc1ce3c80>

两者的区别就在于最外层的[]和(),Lis a list, and g is a generator

Yield generator

>>>defOdd (): ...Print('Step 1')... yield1... Print('Step 2')... yield(3)... Print('Step 3')... yield(5)...>>> o =Odd ()>>>Next (o) Step11>>>Next (o) Step23>>>Next (o) Step35>>>Next (O) Traceback (most recent): File"<stdin>", Line 1,inch<module>stopiteration

Python Advanced Features-builder

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.