![]() ![]() Suppose we have a generator that produces the numbers in the Fibonacci series. This is best illustrated using an example. Multiple generators can be used to pipeline a series of operations. The following generator function can generate all the even numbers (at least in theory). Infinite streams cannot be stored in memory, and since generators produce only one item at a time, they can represent an infinite stream of data. Generators are excellent mediums to represent an infinite stream of data. Generator implementation of such sequences is memory friendly and is preferred since it only produces one item at a time. This is an overkill, if the number of items in the sequence is very large. Memory EfficientĪ normal function to return a sequence will create the entire sequence in memory before returning the result. Since generators keep track of details automatically, the implementation was concise and much cleaner. ![]() Now, let's do the same using a generator function. The above program was lengthy and confusing. Following is an example to implement a sequence of power of 2 using an iterator class. ![]() Generators can be implemented in a clear and concise way as compared to their iterator class counterpart. There are several reasons that make generators a powerful implementation. Here, we have created the generator object that will produce the squares of the numbers 0 through 4 when iterated over.Īnd then, to iterate over the generator and get the values, we have used the for loop. # iterate over the generator and print the values Squares_generator = (i * i for i in range(5)) The generator expression creates a generator object that produces the values of expression for each item in the iterable, one at a time, when iterated over.Įxample 2: Python Generator Expression # create the generator object Here, expression is a value that will be returned for each item in the iterable. Generator Expression SyntaxĪ generator expression has the following syntax, (expression for item in iterable) It is similar to a list comprehension, but instead of creating a list, it creates a generator object that can be iterated over to produce the values in the generator. In Python, a generator expression is a concise way to create a generator object. We can also create a generator object from the generator function by calling the function like we would any other function as, generator = my_range(3) The for loop iterates over the generator object produced by my_generator(), and the print statement prints each value produced by the generator. The yield keyword is used to produce a value from the generator and pause the generator function's execution until the next value is requested. In the above example, the my_generator() generator function takes an integer n as an argument and produces a sequence of numbers from 0 to n-1. # iterate over the generator object produced by my_generator # produce the current value of the counter Here's an example of a generator function that produces a sequence of numbers, def my_generator(n): Instead, it returns a generator object that can be iterated over to produce the values. When the generator function is called, it does not execute the function body immediately. Here, the yield keyword is used to produce a value from the generator. In Python, similar to defining a normal function, we can define a generator function using the def keyword, but instead of the return statement we use the yield statement. Generators are useful when we want to produce a large sequence of values, but we don't want to store all of them in memory at once. In Python, a generator is a function that returns an iterator that produces a sequence of values when iterated over.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |