[Python-ideas] Possible PEP 380 tweak
Guido van Rossum
guido at python.org
Sat Oct 30 05:11:24 CEST 2010
On Mon, Oct 25, 2010 at 6:35 PM, Jacob Holm <jh at improva.dk> wrote:
> I had some later suggestions for how to change the expansion, see e.g.
> http://mail.python.org/pipermail/python-ideas/2009-April/004195.html (I
> find that version easier to reason about even now 1½ years later)
I like that style too. Here it is with some annotations.
_i = iter(EXPR)
_m, _a = next, (_i,)
# _m is a function or a bound method;
# _a is a tuple of arguments to call _m with;
# both are set to other values further down
while 1:
# Move the generator along
try:
_y = _m(*_a)
except StopIteration as _e:
_r = _e.value
break
# Yield _y and process what came back in
try:
_s = yield _y
except GeneratorExit as _e:
# Request to exit
try:
# NOTE: This _m is unrelated to the other
_m = _i.close
except AttributeError:
pass
else:
_m()
raise _e # Always exit
except BaseException as _e:
# An exception was thrown in; pass it along
_a = sys.exc_info()
try:
_m = _i.throw
except AttributeError:
# Can't throw it in; throw it back out
raise _e
else:
# A value was sent in; pass it along
if _s is None:
_m, _a = next, (_i,)
else:
_m, _a = _i.send, (_s,)
RESULT = _r
I do note that this is a bit subtle; I don't like the reusing of _m
and it's hard to verify that _m and _a are set on every path that goes
back to the top of the loop.
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list