[Python-Dev] PEP 340 -- concept clarification

Phillip J. Eby pje at telecommunity.com
Wed May 4 02:05:23 CEST 2005


At 04:41 PM 5/3/05 -0700, Guido van Rossum wrote:
>Given all that, it's not infeasible to add a close() method to
>generators as a shortcut for this:
>
>     def close(self):
>         try:
>             self.__exit__(StopIteration)
>         except StopIteration:
>             break
>         else:
>             # __exit__() didn't
>             raise RuntimeError("or some other exception")
>
>I'd like the block statement to be defined exclusively in terms of
>__exit__() though.

Sure.  PEP 325 proposes a "CloseGenerator" exception in place of 
"StopIteration", however, because:

     """
     Issues: should StopIteration be reused for this purpose?  Probably
     not.  We would like close to be a harmless operation for legacy
     generators, which could contain code catching StopIteration to
     deal with other generators/iterators.
     """

I don't know enough about the issue to offer either support or opposition 
for this idea, though.



More information about the Python-Dev mailing list