On 26 November 2014 at 21:44, Petr Viktorin <encu...@gmail.com> wrote: > On Wed, Nov 26, 2014 at 12:24 PM, Nick Coghlan <ncogh...@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 | ncogh...@gmail.com | Brisbane, Australia _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com