[Python-Dev] Please reconsider PEP 479.

Nick Coghlan ncoghlan at gmail.com
Wed Nov 26 12:52:23 CET 2014


On 26 November 2014 at 21:44, Petr Viktorin <encukou at gmail.com> wrote:
> On Wed, Nov 26, 2014 at 12:24 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Now needs to be written out explicitly as:
>>
>>     def my_generator():
>>         ...
>>        try:
>>             yield next(it)
>>         except StopIteration
>>             return
>>         ...
>
> It could also be written as:
>
>      def my_generator():
>         try:
>             ...
>             yield next(it)
>             ...
>         except StopIteration
>              return
>
> i.e. put the try-block around the whole body, not just the individual
> yield. This emulates what's happenning in current Python, and it would
> be faster than individual try blocks.

Not appreciably though - the potentially slow part that the status quo
avoids is the "catch old exception, return from frame, create and
raise new exception" step, and that only happens once per generator
termination, regardless of how many times the try/except/return
pattern appears in the body of a particular generator.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list