SUM = 0 def test(): global SUM for i in range(10): gen = inner_test() gen.send(None) try: gen.send(None) except StopIteration as ex: SUM += ex.args[0] def inner_test(): yield return 1 test() print(SUM, 'should be == 10') import gc gc.collect() print(len([obj for obj in gc.get_objects() if isinstance(obj, StopIteration)]), 'should be == 0')