Question

In: Computer Science

Describe the characteristics of situations when generators are a good solution? python

Describe the characteristics of situations when generators are a good solution? python

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

On one level, you can think of a Python generator as (among other things) a readable shortcut for creating iterators. Here's gen_nums again:

def gen_nums():
    n = 0
    while n < 4:
        yield n
        n += 1

If you couldn't use "yield," how would you make an equivalent iterator? Just define a class like this:

class Nums:
    MAX = 4
    def __init__(self):
        self.current = 0
    def __iter__(self):
        return self
    def __next__(self):
        next_value = self.current
        if next_value >= self.MAX:
            raise StopIteration
        self.current += 1
        return next_value

Yikes. An instance of this works just like the generator object above…

nums = Nums()
for num in nums:
    print(num)

>>> nums = Nums()
>>> for num in nums:
...     print(num)
0
1
2
3

…but what a price we paid. Look at how complex that class is, compared to the gen_nums function. Which of these two is easier to read? Which is easier to modify without screwing up your code? Which has the key logic all in one place? For me, the generator is immensely preferable.

And it illustrates the encapsulation. It provides new and useful ways for you to package and isolate internal code dependencies. You can do the same thing with classes, but only by spreading state across several methods, in a way that's not nearly as easy to understand.

Kindly revert for any queries

Thanks.


Related Solutions

Use Python: Susie is learning arithmetic, but she’s not so good at it yet. When the...
Use Python: Susie is learning arithmetic, but she’s not so good at it yet. When the teacher writes down the sum of several numbers together, like 1 + 3 + 2 + 1, Susie has to rearrange the numbers into ascending order before adding them. For example, she would rearrange the previous example to 1 + 1 + 2 + 3 before doing the math. Complete the function math help(problem) that takes a string representing a math problem and rearranges...
Describe a time when your mother was a good listener.
Describe a time when your mother was a good listener.
When classifying a good as public, private, or semi-private, there are two important characteristics. What are...
When classifying a good as public, private, or semi-private, there are two important characteristics. What are they, and what do they mean?
1. When creating your own functions, which of the following are good characteristics that make them...
1. When creating your own functions, which of the following are good characteristics that make them robust, sharable, and reusable (check all that apply) the function's name is suggestive of its purpose and/or actions and includes a summary docstring the function freely uses variables in the global namespace to perform its activities the function only uses variables it is passed by calling code or it otherwise internally creates the function only performs a limited number of actions 2. Following list...
What happens to the market for generators in areas hit by a hurricane when the price...
What happens to the market for generators in areas hit by a hurricane when the price gouging law is binding? What is the unintended consequence of this law? In Spring 2020, there were reports of individuals buying up all the hand sanitizer and then trying to sell it for a very high price on Amazon. How is the experience in 2020 different than the one with a hurricane. Briefly explain Farm Aid programs can ensure that prices to agricultural producers...
What are the characteristics of a good strategic planner?
What are the characteristics of a good strategic planner?
characteristics of a good leader - Total Quality Management
Describe the characteristics of a good leader how important is it for the good leader and how good leader put influence on others
What are some important characteristics of a good mentor?
What are some important characteristics of a good mentor?
What are the differences between preferred and common stock? Describe some situations when common stock is...
What are the differences between preferred and common stock? Describe some situations when common stock is a better choice for investors.
Describe the characteristics of an adjuvant.
Describe the characteristics of an adjuvant.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT