Hướng dẫn python generator geeksforgeeks
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Prerequisites: Yield Keyword and Iterators There are two terms involved when we discuss generators. Generator-Function: A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function. Python3
Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop (as shown in the above program). Python3
Có thể bạn quan tâm
So a generator function returns an generator object that is iterable, i.e., can be used as an Iterators . As another example, below is a generator for Fibonacci Numbers. Python3
Output 0 1 1 2 3 Using for in loop 0 1 1 2 3 Applications: Suppose we create a stream of Fibonacci numbers, adopting the generator approach makes it trivial; we just have to call next(x) to get the next Fibonacci number without bothering about where or when the stream of numbers ends. A more practical type of stream processing is handling large data files such as log files. Generators provide a space-efficient method for such data processing as only parts of the file are handled at one given point in time. We can also use Iterators for these purposes, but Generator provides a quick way (We don’t need to write __next__ and __iter__ methods here). This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. View Discussion Improve Article Save Article View Discussion Improve Article Save Article In Python, to create iterators, we can use both regular functions and generators. Generators are written just like a normal function but we use yield() instead of return() for returning a result. It is more powerful as a tool to implement iterators. It is easy and more convenient to implement because it offers the evaluation of elements on demand. Unlike regular functions which on encountering a return statement terminates entirely, generators use a yield statement in which the state of the function is saved from the last call and can be picked up or resumed the next time we call a generator function. Another great advantage of the generator over a list is that it takes much less memory. In addition to that, two more functions _next_() and _iter_() make the generator function more compact and reliable. Example : Python3
Output : First result is 1 Second result is 2 Third result is 3 Difference between Generator function and Normal function –
There are various other expressions that can be simply coded similar to list comprehensions but instead of brackets we use parenthesis. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expression allows creating a generator without a yield keyword. However, it doesn’t share the whole power of the generator created with a yield function. Example : Python3
Output : 0 1 4 9 16 25 36 49 64 81 We can also generate a list using generator expressions : Python3
Output: ['k', 'e', 'e', 'g'] This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. |