Python Fourth: Generators and iterators

Source: Internet
Author: User

One: Generator:generator is a function with the next method,

A function generates an iterator during execution, which is the generator, where the contents of the iterator need to be read using the function __next__ () method, such as:

deffunc (): With open ("haproxy.cfg") as F:Try:#during normal execution.             whileTrue:line=next (f)Print(line,end="')        exceptStopiteration:#catches to the specified exception thrown            Print("has been completed") func () execution Result:GlobalLog127.0.0.1local2 Daemon Maxconn256Log127.0.0.1local2 infodefaults LogGlobalmode HTTP Timeout connect 5000ms timeout client 50000ms timeout server 50000ms opt Ion Dontlognulllisten Stats:8888stats enable stats URI/Admin Stats Auth admin:1234frontend oldboy.org Bind0.0.0.0:80option httplog option httpclose option forwardfor logGlobalACL www hdr_reg (host)-i www.oldboy.org use_backend www.oldboy.orgifwwwbackend www.oldboy.org Server100.1.7.9 100.1.7.9 Weight Maxconn 3000Backend AAA Server100.1.7.9 100.1.7.91 Weight 20maxconn 3000backend test.oldboy.org Server100.1.7.999 100.1.7.999 Weight Maxconn 30Backend test.oldboy.org Server100.1.7.33 100.1.7.33 Weight Maxconn 30Server1.1.1.1 Weight Maxconn 30Server100.1.7.999 100.1.7.999 Weight Maxconn 30The file has been read completed-#after capturing the stopiteration error that was thrown after the next () method was executed without a readable line, the code underneath it was executed
Next ()

The device cannot go backwards in operation, only forward

Python Fourth: Generators and iterators

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.