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 the use of the yield keyword in Python

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

Share

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

Today, I will talk to you about the use of yield keywords in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Iterator

When you create a list, you can read its items one by one. Reading item by item is called iteration:

Mylist is an iterative object. When you use a list parser, you create a list, so it is also an iterator:

So you can use "for... in." Are iterators, including lists, strings, files. Wait.

These iterators are very convenient because you can read them as you like, but you store all the values in memory, which is a waste of memory when you have a lot of values.

To solve this problem, Python has the concept of generator.

Generator

A generator is an iterator, which can only be iterated once. The generator does not store all values in memory, they generate them dynamically:

It is similar to a list parser, except that it uses () instead of []. However, you can't execute I twice in mygenerator, because the generator can only use it once: it print (0), then forget it, print (1), and finally 4.

Yield

Yield is a keyword similar to return, except that the function returns a generator.

After reading the following example carefully, you should be able to fully understand.

The function returns a set of values that need to be read only once. If you can understand this feature clearly and apply it to your code, it may greatly improve performance, and next time we will show you when to use it.

Notice line 6 in the example that the code written in the function body does not run when the function is called. The function only returns the generator object, but don't forget this point.

Finally, your code will continue from where it stops each time you use the generator. So when we use the generator for the second time in the example, our generator has no value at all.

So the core logic is as follows:

1. When the for function first calls the generator object created from the function, it runs the code in the function from scratch until it reaches yield, returning the first value of the loop.

two。 Subsequent calls will run the loop you wrote in the function again, and yield will return the next value until there is no value to return

After reading the above, do you have any further understanding of the use of yield keywords in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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