[Python-Dev] PEP 380 (yield from a subgenerator) comments

Steve Holden steve at holdenweb.com
Wed Mar 25 03:22:59 CET 2009


Greg Ewing wrote:
> Guido van Rossum wrote:
> 
>> I really don't like to have things whose semantics is
>> defined in terms of code inlining -- even if you don't mean that as
>> the formal semantics but just as a mnemonic hint.
> 
> Think about it the other way around, then. Take any chunk
> of code containing a yield, factor it out into a separate
> function (using whatever techniques you would normally
> use when performing such a refactoring to deal with
> references to variables in the surrounding scope) and
> call it using yield-from. The result should be the same
> as the original unfactored code.
> 
> That's the fundamental reason behind all of this -- to
> make such refactorings possible in a straightforward way.
> 
>> What happened to the first-order approximation
>>
>>   "yield from X" means roughly the same as "for _x in X: yield x"
> 
> Everybody's reaction to that when it's been suggested
> before has been "that's trivial, why bother?" So I've
> been trying to present it in a way that doesn't make it
> appear so trivial.
> 
There is one non-trivial extension that I've been chewing over for a
while. What if you want to yield not the values from the generator but
some function of those values? The present proposal appears to have no
way to specify that. What about extending the syntax somewhat to

  yield expr for x from X

The idea is that x should be a a bound variable in expr, but the "expr
for x" could be optional to yield the existing proposal as a degenerate
case.

> Also [...]

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
Holden Web LLC                 http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/



More information about the Python-Dev mailing list