Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of send method in python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the example of the send method in python". In the daily operation, I believe that many people have doubts about the example of the send method in python. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "the example of send method in python". Next, please follow the editor to study!

Yield from takes a value from the container

Yield from takes values from the container, which contains generators, which means that yield from asks one generator to delegate some of its operations to another.

Def reverse (x): while Xmur1 > = 0: yield Xmur1 x-= 1def list_number (x): yield from range (x) yield from range (xmurl 1,-1,-1) yield from reverse (x) for i in list_number (3): print (I endure endurance) out:0,1,2,2,1,0,2,1,0, case on send method def count (): # sub-generator, accumulating the numbers from yield If the value passed is None, the cumulative result result = 0 while 1: X = yield if x is None: return result result + = xdef count_list (x): # parent generator is returned Delegate the cumulative task to the sub-generator while 1: y = yield from count () x.append (y) plus_list = [] cal = count_list (plus_list) next (cal) # so that the generator can receive the incoming value for i in range (1,5): cal.send (I) cal.send (None) # it is the count function that actually receives the None End accumulation for i in range (1,11): cal.send (I) cal.send (None) # end second accumulation print (plus_list) # output final result out: [10,55] two points for attention of send method

The send method is used with the yield statement.

The send method can pass a value to yield, but it will report an error when the yield has not generated a breakpoint, so you have to next the generator before you can pass the value normally.

In-depth understanding of send and yield from mechanisms

The combination of send and yield from can form a transparent two-way data channel between the caller and the generator, including obtaining data from the generator and transmitting data to the generator.

To have a deeper understanding of send and yield mechanism, we need to learn and understand the knowledge points related to the collaborative process. I will talk about send and yield from in detail when I sort out the knowledge points of the cooperative process in the future.

At this point, the study of "examples of send methods in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report