Consider the following (non-working) example code:
class MyGenerator: def test_gen(self): for i in range(1,5): if i % 2: self.foo(i) else: self.bar(i) def foo(self, i): yield i def bar(self, i): yield i**2 g = MyGenerator() for i in g.test_gen(): print i This will not work, because test_gen has no yield and is no longer a generator function. In this small example I could just return the values from foo and bar and put the yield into test_gen, however I have a case where that's not possible. How can I turn test_gen into a generator function again?