Distinguishing active generators from exhausted ones

Michal Kwiatkowski constant.beta at gmail.com
Mon Jul 27 15:46:01 CEST 2009


On Jul 27, 1:56 am, a... at pythoncraft.com (Aahz) wrote:
> >> 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.

No, it does not work in Python 2.4. gi_frame can be None only in
Python 2.5 and higher.

Via "What’s New in Python 2.5" (http://docs.python.org/whatsnew/
2.5.html):

"""
Another even more esoteric effect of this change: previously, the
gi_frame attribute of a generator was always a frame object. It’s now
possible for gi_frame to be None once the generator has been
exhausted.
"""

Cheers,
mk



More information about the Python-list mailing list