Python 12th Day

Source: Internet
Author: User

1. Generator

The essence of the generator is the iterator,

The simplest generator function is as follows:

1 deffoo (x): 1-4 rows defines a simple generator function2     yieldX+1 yield and return are different, return ends the entire function3     yieldX+2 2, 3, 4 yield and 8, 9, 10 __next__ one by one corresponding4     yieldX+35 6 7f = Foo (7) to create a generator object, assign a value to the variable F8 Print(F.__next__()) F calls a __next__ once to execute a yield in the function,9 Print(F.__next__()) Note One-time downward executionTen Print(F.__next__())

The results of the program run as follows:

8910

Note that if you call __next__ again, the program will error

Python 12th Day

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.