Is it acceptable/Pythonic to use a method in a class as a generator? All the examples I have found show the yield statement in a function, not in a class.
Here is an example working code:
for i in range(10):
for i in self.first_ten():
Yes, this is perfectly normal. For example, it is commonly used to implement an
class SomeContainer(object): def __iter__(self): for elem in self._datastructure: if elem.visible: yield elem.value
However, don't feel limited by that common pattern; anything that requires iteration is a candidate for a generator method.