Python Advanced Programming Builder (Generator) and Coroutine (i): Generator

Source: Internet
Author: User

This is a series of articles, starting from the basics, and introducing the generator in Python and coroutine (mainly about Coroutine), and detailing the various advanced uses of coroutine in Python, Finally, a simple multi-tasking operating system is implemented with Coroutine.

Actually also read this article the study note! O (∩_∩) o

Generator (Generator)

What is a generator? In Python, the generator (Generator) is a function with the yield keyword

1 defgene ():2A = 13     Print "start executing gene (), a =", a4A + = 15     yield "rio_2607"6     Print "go back to Gene (), then quit and start executing, a =", a7A + = 28     yield "UESTC"9     Print "back to Gene (), a =", aTen     yield "EMC"

Gene () is a generator because the definition of a function has the yield keyword. So what's the difference between a generator and a normal function?

When the gene () function is called, the code in the gene () function is not executed immediately, but instead returns a Generator object (Generator objects):

1>>>defgene ():2A = 13     Print "start executing gene (), a =", a4A + = 15     yield "rio_2607"6     Print "go back to Gene (), then quit and start executing, a =", a7A + = 28     yield "UESTC"9     Print "back to Gene (), a =", aTen     yield "EMC" One  A      ->>> g =gene () ->>>type (g) the<type'Generator'>

As you can see, G is an object of type generator. So when will the code of the function be executed? A: When you call the next () function of the Builder object, the code in the function definition starts executing.

But as soon as the normal function starts executing and executes until it ends, the generator function goes down, but once it touches the yield keyword, it returns the data after the yield keyword, holds all the current state of the function, and pauses the execution of the function. When the next () function of the generator object is called again, it continues until the last pause, until the code of the next yield keyword or function finishes executing

 >>> g = gene ()  >>> type (g  <type  " generator   '  >>>> 

As you can see, the code inside the function starts executing the first time the G.next () function is called, and when executed to the yield "rio_2607" code, it returns "rio_2607" and the function pauses execution. Then when the next function is called again, the gene () function will proceed to the following, you can see that the print out of the a=2, keep the function last time the data, when the yield "UESTC" This sentence, the function will stop executing, the data in the function is sealed. When the next () function is called again, Gene () proceeds to the last state, where the last pause continues, and you can see that the print output is a=4 and the yield is paused again after execution.

When the generator finishes executing and calls next () again, the function throws a Stopiteration exception

>>>=  4'EMC'>>> g.next () Traceback (most recent call Last):  '<pyshell#13>' in <module>     G.next () stopiteration

Generator expression (Generator expresisions)

The generator expression (Generator expresisions) is similar to a list-derived formula (list comprehension)

 for  in a)

WHERE (x * 2 for x in a) is the generator expression, which returns a generator object:

 for inch a) >>> GE<generator object <genexpr> at 0x01ea0a30>

In the For loop, the for loop automatically invokes the next () function of the builder object and handles the stopiteration exception:

>>> ge for in ge:    print  i    2468

Having said so much, what does the generator do in addition to implementing iterators (iteration)?

We have a log file above the Web server, which is probably the data

77.81.4.30--[24/feb/2008:02:17:53-0600]"Get/favicon.ico http/1.1"404 13324.1.247.118--[24/feb/2008:02:20:25-0600]"get/dynamic/http/1.1"510524.1.247.118--[24/feb/2008:02:20:26-0600]"Get/favicon.ico http/1.1"404 13324.1.247.118--[24/feb/2008:02:20:26-0600]"Get/favicon.ico http/1.1"404 133122.117.168.219--[24/feb/2008:02:22:06-0600]"get/ply/http/1.1"304-122.117.168.219--[24/feb/2008:02:22:06-0600]"get/ply/bookplug.gif http/1.1"304-122.117.168.219--[24/feb/2008:02:22:08-0600]"get/ply/example.html http/1.1"304-89.182.136.236--[24/feb/2008:02:23:04-0600]"get/ply/http/1.1"801889.182.136.236--[24/feb/2008:02:23:05-0600]"get/ply/bookplug.gif http/1.1"2390389.182.136.236--[24/feb/2008:02:23:05-0600]"Get/favicon.ico http/1.1"404 13366.249.65.37--[24/feb/2008:02:23:29-0600]"get/papers/siam97/siam97.pdf http/1.1"188949117.198.144.124--[24/feb/2008:02:23:50-0600]"get/ply/ply.html http/1.1"97238117.198.144.124--[24/feb/2008:02:23:53-0600]"Get/favicon.ico http/1.1"404 133

The last column of each row represents either a byte of data or a-that indicates that the byte data is unknown

Now we want to count all the byte data sizes recorded in the file.

The usual way to do this in Python is to process one row of data at a time in a For loop:

1 defNon_generator_func ():2     " "3 Parse Web server's log file to determine the number of bytes transferred4 Non-generator: Using a For loop5 : return:6     " "7Wwwlog = open ("Access-log")8Total =09 Ten      forLineinchWwwlog: One         #gets the string representation of the number of bytes ABytestr = Line.rsplit (None, 1) [1] -         ifBytestr! ="-": -Total + =Int (BYTESTR) the  -     Print " Total", total

Now let's look at the code written in generator style:

1 defGenerator_func ():2Wwwlog = open ("Access-log")3     #returns a Generator object using the generator expression (Generator expression)4Bytecolumn = (Line.rsplit (None, 1) [1] forLineinchWwwlog)5bytes = (int (x) forXinchBytecolumnifX! ="-")6 7     #The final step is calculated8     Print " Total", sum (bytes)

As you can see, with generator, you can write less code and have a completely different programming style than normal Python programming.

About the builder in Python, Python Functional Programming Guide (iv): Generator This blog is very good, you can read this blog.

Python Advanced Programming Builder (Generator) and Coroutine (i): Generator

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.