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

Jacob Holm jh at improva.dk
Sun Apr 19 16:34:02 CEST 2009


Nick Coghlan wrote:
> Jacob Holm wrote:
>> FWIW I still consider an expansion using functools.partial to be more
>> readable because it centralizes the StopIteration handling and reduces
>> the code nesting.  Here is an updated version of such an expansion that
>> is semantically equivalent to the PEP rev 13 + my suggested fix for the
>> sys.exc_info() issue:
> 
> Nobody would ever implement it that way though - using partial like that
> in the formal semantic definition implies a whole heap of temporary
> objects that just won't exist in practice.
> 
> Better to use the more verbose expansion that is much closer in spirit
> to the way it would actually be implemented.

The temporary objects don't bother me.  That is really a deep 
implementation detail.  As for being closer in spirit, the real 
implementation is already going to be very different so I don't see that 
as a problem either.

FWIW, it is quite easy to write same style expansion without using 
functools.partial, like this:

     _i = iter(EXPR)
     _m, _a = next, (_i,)
     while 1:
         try:
             _y = _m(*_a)
         except StopIteration as _e:
             _r = _e.value
             break
         try:
             _s = yield _y
         except GeneratorExit as _e:
             try:
                 _m = _i.close
             except AttributeError:
                 pass
             else:
                 _m()
             raise _e
         except BaseException as _e:
             _a = sys.exc_info()
             try:
                 _m = _i.throw
             except AttributeError:
                 raise _e
         else:
             if _s is None:
                 _m, _a = next, (_i,)
             else:
                 _m, _a = _i.send, (_s,)
     RESULT = _r

This is even a line shorter than the version using functools.partial, 
and the temporary _a tuples used actually match what happens in a normal 
function call anyway (I think)...

Anyway, as I said it is not all that important.  It is a presentation 
detail, and as such very subjective.  I can agree to disagree about what 
version is clearer.

Cheers
- Jacob



More information about the Python-ideas mailing list