Advanced Python, Generators
What are generators?
Building an iterator in python involves a lot of overhead. You have to:
- Implement a class with _iter_ ( ) and method of _next_
- Keep track of internal states
- Raise StopeIteration function when there are no values to be returned, etc.
This is both a counter-intuitive and lengthy procedure. However, generators provide a simple way of creating iterators in Python. They automatically handle all the overheads we have mentioned above. We can define a generator as a function that returns an object which can be iterated over one value at a time.
How are generators created in Python?
Creating a generator in Python is as simple as defining a normal function with a yield statement instead of a return statement. A function that contains at least one yield statement automatically becomes a generator function. Both the return and yield statements return some value from a function. The main difference between these two is that a yield statement pauses a function, saving all its states. Later, it continues from there on successive calls. On the other hand, a return statement terminates a function entirely.
What are the differences between a generator function and a normal function?
Our python programming assignment helpers have highlighted how a normal function differs from a generator function below:
- A generator function has one or more yield statement
- When a generator function is called, it does not immediately start execution. Also, it returns an object (iterator)
- A generator function allows programmers to iterate through items using the next(). Methods like _next()_ and _iter() are automatically implemented.
- A generator function is paused after it yields. Control is then transferred to the caller.
- A generator function remembers local variables and their states between successive calls.
- In generators, StopIteration is automatically raised on further calls when the function terminates.
You should also note that generators can be used with for loops directly. The reason is a for loop takes an iterator and iterates over it using the next() function. Additionally, it automatically ends when StopIteration is raised.
Generators with Loops
Python generators are normally implemented with a loop that has a suitable terminating condition. For example, we can use the range(), it is a generator that reverses a string to get the index in reverse order using the for loop
Python Generators Expressions
We can use generator expressions to create simple generators on the fly. This process makes building generators quick and hassle-free. Just like the lambda function can be used to create an anonymous function, a generator expression can be used to create an anonymous generator function. The syntax used for a generator expression is the same as that used in a list comprehension in python. The main difference is that we replace the square brackets with round parentheses. The other difference is that list comprehensions produces the entire list while generator expressions produce one item at a time.
Why are generators used in Python?
There are a myriad of reasons why generators are used in Python. We have discussed some of them below:
- Generators are easy to implement
Python generators have a concise and clear way of implementation as compared to their iterator class counterpart. They automatically keep track of the details.
- They are memory-efficient
When we use a normal function to return a sequence, it will create the entire sequence in memory before returning the result. This can be a time-consuming and overkill if the items in the sequence are very large. Generators are very memory friendly when dealing with such a sequence. For this reason, they are highly regarded since they produce one item at a time.
- Generators represent an infinitive stream
Python generators offer an excellent medium for representing an infinite stream of data that cannot be stored in memory. Since generators produce only one item at a time, they are the best medium for representing an infinite stream of data.
- Generators and Pipelining
We can also use generators to pipeline a series of operations. To best illustrate this, let us use the example below:
Suppose we are in charge of maintaining the log file of a famous fast-food chain. On the fourth column of our log, we have a track of all the pizza sold every hour. We want to calculate the total number of pizzas the fast-food chain has sold in five years:
- The missing numbers are marked as N/A
- Everything is in string
We can use a generator implementation as follows:
With open (‘sales.log’) as file
Pizza_col = (line  for line in file
per_hour = (int (x) for x in pizza _col if x ! = ‘N/A’)
print (“The total pizzas sold in five years =”, sum (per-hour)
Hire our python homework help experts for first-class assistance with python related tasks.