Oops. Got the indentation wrong. Here is the corrected version: def generator(self): while True: while True: if (yield 1): if (yield 1): break self.n = 2 while (yield self.n): self.n += 1