What would be the best way to stop a generator? Example:
>>> def get_fruits():
fruits = ['Mamão', 'Abacate', 'Melão', 'Banana', 'Maçã', 'Uva']
for fruit in fruits:
yield fruit
>>> my_bag = []
>>> salesman_fruits = get_fruits()
>>>
>>> for fruit in salesman_fruits:
my_bag.append(fruit)
if fruit is 'Banana':
salesman_fruits.close() # Chega não quero mais frutas
This was the way I found to stop the gnnator from outside it, ie, for the generator to run while until the "user" says it does not want it anymore. My question is I do not know if the generetor's close () method is for this purpose or I'm misusing it.