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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4