[Python-ideas] Revised**11 PEP on Yield-From

Scott David Daniels Scott.Daniels at Acm.Org
Sun Apr 19 23:28:53 CEST 2009


Greg Ewing wrote:
> Draft 12 of the PEP....

In the ensuing discussion, I replied to one of Jacob Holm's points
by making a comment that was really meant to apply to the PEP itself,
rather than his suggestion.  I replied at the place where it became
obvious to me how I felt the PEP could become clearer.  In an off-line
exchange, I found he thought I was talking simply about his suggested
change, so I'll restate my case here.  I do so not for emphasis, but
rather to attach it to the proper context.  This is, of course,
Greg's decision for readability.  I share Jacob's fear of bike-shed
discussions on variable names (I am not necessarily in love with
the names I've chosen here).

Now that we passed the magic three or four threshold, is
it not easier to read if we pick some better names?
Instead of:
 >    _i = iter(EXPR)
 >    try:
 >        _y = next(_i)
 >    except StopIteration as _e:
 >        _r = _e.value
 >    else:
 >        while 1:
 >            try:
 >                _s = yield _y
 >            except GeneratorExit:
 >                _m = getattr(_i, 'close', None)
 >                if _m is not None:
 >                    _m()
 >                raise
 >            except:
 >                _m = getattr(_i, 'throw', None)
 >                if _m is not None:
 >                    try:
 >                        _y = _m(*sys.exc_info())
 >                    except StopIteration as _e:
 >                        _r = _e.value
 >                        break
 >                else:
 >                    raise
 >            else:
 >                try:
 >                    if _s is None:
 >                        _y = next(_i)
 >                    else:
 >                        _y = _i.send(_s)
 >                except StopIteration as _e:
 >                    _r = _e.value
 >                    break
 >    RESULT = _r

we could use:
     _iterator = iter(EXPR)
     try:
         _out = next(_iterator)
     except StopIteration as _error:
         _result = _error.value
     else:
         while 1:
             try:
                 _inner = yield _out
             except GeneratorExit:
                 _close = getattr(_i, 'close', None)
                 if _close is not None:
                     _close()
                 raise
             except:
                 _throw = getattr(_iterator, 'throw', None)
                 if _throw is not None:
                     try:
                         _out = _throw(*sys.exc_info())
                     except StopIteration as _error:
                         _result = _error.value
                         break
                 else:
                     raise
             else:
                 try:
                     if _inner is None:
                         _out = next(_iterator)
                     else:
                         _out = _i.send(_inner)
                 except StopIteration as _error:
                     _result = _error.value
                     break
     RESULT = _result


--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-ideas mailing list