Python functions and common modules-generators and operators

Source: Internet
Author: User

At the moment we already know the generator in general, but how to actually use it?! And then we'll take an example.

Yield preserves the state of this function, returns the value of the current state, and stops the function here, and when it comes back, it is time to perform it back to execute.

In the case of yield, the results are actually calculated.

#!/usr/bin/env python3# -*- coding:utf-8 -*-def consumer(name):    print("%s 準備吃包子啦!" %name)    while True:       baozi = yield  # baozi 沒有返回值就會為none       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))        c = consumer("Tony") c.__next__()             ---------------執行結果---------------Tony 準備吃包子啦!Process finished with exit code 0

Generate a consumer called Tony, in fact the code is a generator, so to use __next__() this method to adjust, but only one __next__() method, the program will stop on the end baozi = yield , then the following will not be executed, then if you want to continue to execute the following words, how to do?!

In fact, it's easy to adjust the method once __next__() .

#!/usr/bin/env python3# -*- coding:utf-8 -*-def consumer(name):    print("%s 準備吃包子啦!" %name)    while True:       baozi = yield  # baozi 沒有返回值就會為none       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))        c = consumer("Tony") c.__next__() c.__next__()            ---------------執行結果---------------Tony 準備吃包子啦!包子[None]来了,被[Tony]吃了!Process finished with exit code 0

Yes! Steamed buns were printed out, note that the buns become none, and this none is to represent the bun as empty, of course there is no steamed buns to eat, then how to do a bun to Tony?

#!/usr/bin/env python3# -*- coding:utf-8 -*-# import timedef consumer(name):    print("%s 準備吃包子啦!" %(name))    while True:       baozi = yield       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))c = consumer("Tony")c.__next__()b1 = "韭菜餡"c.send(b1)---------------執行結果---------------Tony 準備吃包子啦!包子[韭菜餡]来了,被[Tony]吃了!Process finished with exit code 0

We made a stuffed bun and, through b1 = "韭菜餡" send() This method, gave Tony buns a bite to eat.

    • send()The fact is to adjust yield, and also to give it a value.
    • __next__()Just to adjust yield, and not to pass the value to yield.

Look at the code below to get a clearer picture of

#!/usr/bin/env python3# -*- coding:utf-8 -*-import timedef consumer(name):    print("%s 準備吃包子啦!" %(name))    while True:       baozi = yield       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))c = consumer("Tony")c.__next__()b1 = "韭菜餡"c.send(b1)c.__next__()---------------執行結果---------------Tony 準備吃包子啦!包子[韭菜餡]来了,被[Tony]吃了!包子[None]来了,被[Tony]吃了!Process finished with exit code 0

To observe, there is no 第二個__next__() value of the discovery, only to adjust.

Has it ever happened that the above code looks like a two-task interaction, so next we're going to make the process of making buns a little bit.

  #!/usr/bin/env python3#-*-coding:utf-8-*-import timedef Consumer (name): Print ("%s ready to eat buns!"% (name)) WH  Ile True:baozi = yield print ("bun [%s] came, was [%s] eaten!"% (Baozi,name)) def producer (name): c = Consumer (' A ') c2    = Consumer (' B ') c.__next__ () c2.__next__ () print ("Lao Tzu began to prepare buns!")        For I in range: Time.sleep (1) print ("Make 1 buns in two halves!") C.send (i) c2.send (i) producer ("Tony")---------------perform the results---------------A ready to eat buns! B 準備吃包子啦!老子开始準備做包子啦!做了1個包子分兩半!包子[0]来了,被[A]吃了!包子[0]来了,被[B]吃了!做了1個包子分兩半!包子[1]来了,被[A]吃了!包子[1]来了,被[B]吃了!做了1個包子分兩半! 包子[2]来了,被[A]吃了!包子[2]来了,被[B]吃了!做了1個包子分兩半!包子[3]来了,被[A]吃了!包子[3]来了,被[B]吃了!做了1個包子分兩半!包子[4]来了,被[A]吃了!包子[4]来了,被[B]吃了! 做了1個包子分兩半!包子[5]来了,被[A]吃了!包子[5]来了,被[B]吃了!做了1個包子分兩半!包子[6]来了,被[A]吃了!包子[6]来了,被[B]吃了!做了1個包子分兩半!包子[7]来了,被[A]吃了!包子[7] 来了,被[B]吃了!做了1個包子分兩半!包子[8]来了,被[A]吃了!包子[8]来了,被[B]吃了!做了1個包子分兩半!包子[9]来了,被[A]吃了!包子[9]来了,被[B]吃了! Process finished with exit code 0  

The effect on a single line is coming out, which is actually serial, because it can be switched in different roles, and because the speed of the movement is very fast, it feels like it is, and this is the 菢雛之窩 shape of the "interracial io".

The bottom layer of the epoll is similar to that of the above code, which is essentially the same thing.

What we call the above code, called "The process", can be understood as the simplest process and is a typical basic consumer consumption model, which, simply, is understood as a person's production, one in the consumer.

Python functions and common modules-generators and operators

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.