Python Programming - Generators

Exercise : Generators - General Questions
  • Generators - General Questions
56.
How does the generator.close() method affect a running generator?
It restarts the generator
It terminates the generator by raising a GeneratorExit exception
It pauses the generator's execution temporarily
It has no effect on the running generator
Answer: Option
Explanation:
Calling generator.close() terminates the running generator by raising a GeneratorExit exception. This allows for cleanup operations before the generator is fully closed.
def my_generator():
    try:
        while True:
            yield 1
    except GeneratorExit:
        print("Generator closed")

gen = my_generator()
next(gen)
gen.close()

57.
How does the generator.send(value) method differ from yield value when used with a generator?
They have the same effect on the generator
generator.send(value) sends a value into the generator, while yield value yields a value from the generator
yield value sends a value into the generator, while generator.send(value) yields a value from the generator
generator.send(value) stops the generator, while yield value continues the generator's execution
Answer: Option
Explanation:
generator.send(value) is used to send a value into the generator, while yield value is used to yield a value from the generator.

58.
What is the purpose of the itertools.cycle() function when used with generators?
It creates an infinite sequence of repeated values from the generator
It generates a single iteration from the generator
It stops the generator after one complete cycle
It shuffles the values produced by the generator
Answer: Option
Explanation:
itertools.cycle() creates an infinite sequence of repeated values. When used with a generator, it continuously cycles through the values produced by the generator.
import itertools

def my_generator():
    yield 1
    yield 2
    yield 3

cycled_generator = itertools.cycle(my_generator())

for _ in range(10):
    print(next(cycled_generator))

59.
How does the itertools.islice() function differ from using generator[start:end] slicing?
They are equivalent in functionality
itertools.islice() can be used for any iterable, while slicing is specific to generators
generator[start:end] can be used for any iterable, while itertools.islice() is specific to generators
itertools.islice() modifies the original generator, while slicing creates a new generator
Answer: Option
Explanation:
itertools.islice() can be used for any iterable, not just generators, while slicing with generator[start:end] is specific to generators and creates a new generator.
import itertools

def my_generator():
    for i in range(10):
        yield i

# Using itertools.islice() for slicing
sliced_iterable = itertools.islice(my_generator(), 2, 7)

for value in sliced_iterable:
    print(value)

60.
What is the purpose of the itertools.compress() function when used with generators?
It compresses the generator into a ZIP file
It filters elements based on a boolean mask
It stops the generator after one iteration
It interleaves values from different generators
Answer: Option
Explanation:
itertools.compress() filters elements from the generator based on a boolean mask, which is provided as the second iterable argument.