[Python-ideas] x=(yield from) confusion [was:Yet another alternative name for yield-from]

Guido van Rossum guido at python.org
Fri Apr 10 06:19:09 CEST 2009


On Thu, Apr 9, 2009 at 5:35 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Jacob Holm wrote:
>> That rules out Gregs and my patches as well.  They both need extra state
>> on the frame object to be able to implement yield-from in the first place.
>
> But that state is obviously necessary in order to support
> yield-from, and it goes away as soon as the yield-from
> itself finishes.

Another way to look at this is, "RETVAL = yield from EXPR" has an
expansion into source code where all that state is kept as either a
local variable or via the position in the code, and that's how we
define the semantics. I don't believe Jacob's proposal (the one that
doesn't require new syntax) works this way.

> Your proposals add non-obvious extra state that persists
> longer than normally expected, to support obscure features
> that will rarely be used.
>
>> One final suggestion I have is to make yield-from raise a RuntimeError if
>> used on a generator that already has a frame.  That would ...
>
>> b) make it clear that the only intended
>>
>> use is with a *fresh* generator or a non-generator iterable,
>
> But there's no way of detecting a violation of that rule
> for a non-generator iterator, and I want to avoid having
> special cases for generators, since it goes against duck
> typing.

It also goes against the "for x in EXPR: yield x" expansion, which I
would like to maintain as an anchor point for the semantics. To be
precise: when the .send(), .throw() and .close() methods of the outer
generator aren't used, *or* when iter(EXPR) returns a non-generator
iterator, these semantics should be maintained precisely, and the
extended semantics if .send()/.throw()/.close() are used on the outer
generator and iter(EXPR) is a generator should be natural extensions
of this, without semantic discontinuities.

PS. On Jacob's complaint that he didn't realize earlier that his
proposal was dead on arrival: I didn't either. It took me all this
time to translate my gut feelings into rules.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list