Distinguishing active generators from exhausted ones

Aahz aahz at pythoncraft.com
Mon Jul 27 01:56:59 CEST 2009

In article <2a408da6-af57-45d0-a75f-4cbe384bb7f9 at s15g2000yqs.googlegroups.com>,
Michal Kwiatkowski  <constant.beta at gmail.com> wrote:
>On Jul 25, 10:00=A0pm, Jason Tackaberry <t... at urandom.ca> wrote:
>> On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote:
>>> Is there a way to tell if a generator has been exhausted using pure
>>> Python code? I've looked at CPython sources and it seems that
>> Upon a cursory look, after a generator 'gen' is exhausted (meaning
>> gen.next() has raised StopIteration), it seems that gen.gi_frame will be
>> None.
>Only in Python 2.5 or higher though. I need to support Python 2.3 and
>2.4 as well, sorry for not making that clear in the original post.

Are you sure?  It appears to work in Python 2.4; I don't have time to
check 2.3.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer

More information about the Python-list mailing list