while expression feature proposal
steve+comp.lang.python at pearwood.info
Thu Oct 25 12:50:53 CEST 2012
On Thu, 25 Oct 2012 11:52:31 +0200, Thomas Rachel wrote:
> Am 25.10.2012 06:50 schrieb Terry Reedy:
>> Keep in mind that any new syntax has to be a substantial improvement in
>> some sense or make something new possible. There was no new syntax in
>> 3.2 and very little in 3.3.
> I would consinder this at least as new substantial than
> yield_from it
> as opposed to
> for i in it: yield i
> - although I think that was a good idea as well.
Then I think you have misunderstood the purpose of "yield from". The fact
that you can replace the two lines:
for value in another_iterator:
with a one-liner "yield from another_iterator" is the least important use-
case for yield-from. If that was the only use-case, it probably would not
have been allowed, because it adds complication to the language for a
The purpose of yield-from is to transfer control to another coroutine,
not to save one trivial line of code.
However, if the subgenerator is to interact properly with the caller in
the case of calls to send(), throw() and close(), things become
considerably more difficult. As will be seen later, the necessary code is
very complicated, and it is tricky to handle all the corner cases
A new syntax will be proposed to address this issue. In the simplest use
cases, it will be equivalent to the above for-loop, but it will also
handle the full range of generator behaviour, and allow generator code to
be refactored in a simple and straightforward way.
"yield from" is a *huge* win in terms of correctness and power, not just
a trivial saving in lines of code. "while expr as var" is not.
More information about the Python-list