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

How to implement python Generator

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

Share

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

This article mainly explains "how to implement the python generator". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to implement the python generator".

When it comes to generators, it is inevitable to pull iterators out. In contrast, a generator is an object that is very similar to an iterator in behavior. If you compare an iterator to an Android system, then the generator is iOS, both functionally similar, but the generator is more elegant.

What is an iterator?

As the name implies, an iterator is an object used for iterative operations (for loops). It can iterate over each of these elements like a list, and any implementation of the _ _ next__ method

Objects of (python2 is next) can be called iterators.

It differs from lists in that, unlike lists, which load all elements into memory at once, they return elements in a lazy evaluation way, which is its advantage. For example, the list contains 10 million integers, which takes up more than 400 megabytes of memory, while the iterator only needs dozens of bytes of space. Because it does not load all the elements into memory, but waits until the next method is called (call by need), the for loop is essentially a constant call to the iterator's next method.

Take the Fibonacci number as an example to implement an iterator:

Class Fib: def _ _ init__ (self N): self.prev = 0 self.cur = 1 self.n = n def _ _ iter__ (self): return self def _ next__ (self): if self.n > 0: value = self.cur self.cur = self.cur + self.prev self.prev = value self.n-= 1 Return value else: raise StopIteration () f = Fib (10) print ([i for i in f]) # [1 1,2,3,5,8,13,21,34,55] what is a generator?

Once you know the iterator, you can officially get into the topic of generator. Ordinary functions return a value in return, which is the same as in other languages such as Java, but there is also a function in Python that returns a value with the keyword yield. This function is called a generator function, which returns a generator object when it is called. The generator is essentially an iterator and is also used in iterative operations, so it has the same characteristics as iterators. The only difference is that it is implemented in a different way. The latter is more concise.

The simplest generator function:

Def func (n):

... Yield nasty 2

...

> func

> g = func (5)

> > g

> > >

Func is a generator function, and the return object when calling this function is generator g. The behavior of this generator object is very similar to that of iterators and can be used in scenarios such as for loops. Note that the value corresponding to yield is not returned immediately when the function is called, but only when the next method is called (essentially the for loop also calls the next method)

> g = func (5)

> > next (g)

ten

> g = func (5)

> for i in g:

... Print (I)

...

ten

Then why use a generator? Obviously, using a generator is several levels higher than an iterator on a forced grid, it doesn't have as much verbose code, and it's as efficient in performance, so why not use it? Let's see how easy it is to use the generator to implement the Fibonacci sequence.

Def fib (n):

Prev, curr = 0,1

While n > 0:

N-= 1

Yield curr

Prev, curr = curr, curr + prev

Print ([i for i in fib (10)])

# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] generator expression

In the previous article, which is more elegant in writing code in this way, I introduced list comprehension. The generator expression is very similar to the list deduction, but they return different objects. The former returns the generator object and the latter returns the list object.

> g = (xylene 2 for x in range (10))

> > type (g)

> l = [Xero2 for x in range (10)]

Type (l)

Thank you for your reading, the above is the content of "how to achieve python generator", after the study of this article, I believe you have a deeper understanding of how to achieve this problem of python generator, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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