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

What is yeild in python

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

Share

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

This article introduces the relevant knowledge of "what is yeild in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In python, a function that uses yeild is called a generator, which is called generator.

Def fun (file): for rr in file: if rr = = 'this is an appended file 2\ n' yield rrwith open ("xxx.txt",'r') as f: readlines = f.readlines () with open ('xxx.txt',') as t: for i in fun (readlines): t.write (I)

A function with yield is a generator, which, unlike an ordinary function, generates a generator that looks like a function call, but does not execute any function code until it is called next () (which is automatically called next () in the for loop). Although the execution process still follows the flow of the function, each time a yield statement is executed, it breaks and returns an iterative value, and the next execution continues from the next statement in yield. It looks as if a function has been interrupted by yield several times during normal execution, and each interrupt returns the current iterative value through yield.

The benefits of yield are obvious. Rewriting a function to a generator gains iterative ability. Compared with using an instance of the class to save state to calculate the value of the next next (), not only the code is concise, but also the execution process is extremely clear.

In a generator function, if there is no return, the default execution is to the end of the function, and if return is in the process of execution, a StopIteration is thrown directly to terminate the iteration.

If you want to pass parameters, then use send, the specific operation will be explained next.

This is the end of the content of "what is yeild in python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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