PEP 380 - the 'yield from' proposal

  • Thread starter Hallvard B Furuseth
  • Start date
H

Hallvard B Furuseth

Regarding http://www.python.org/dev/peps/pep-0380/,
"Syntax for Delegating to a Subgenerator":

The first call can only be .next(), there's no way to provide an initial
value to .send(). That matches common use, but an initial .send() is
possible if .next() was called before "yield from". So I suggest:

RESULT = yield from EXPR [with SEND_FIRST] # default SEND_FIRST=None

The code under Formal Semantics uses .throw and .close = None as
equivalent to absent attributes, which is not what the textual
description says.

I think the code should delete values when it no longer has use for
them, so they can be garbage-collected as quickly as possible.

So the formal semantics would be something like

i, snd, yld, throw = iter(EXPR), SEND_FIRST, None, None
res = absent = object() # Internal marker, never exposed
try:
while res is absent:
try:
yld = (i.next() if snd is None else i.send(snd))
except StopIteration as e:
res = e.value
else:
snd = absent # 'del snd', but that could break 'finally:'
while yld is not absent:
try:
snd = yield yld
yld = absent
except:
del yld
if throw is None: # optional statement
throw = getattr(i, 'throw', absent)
if throw is absent:
getattr(i, 'close', bool)() # bool()=dummy
raise
x = sys.exc_info()
try:
yld = throw(*x)
except StopIteration as e:
if e is x[1] or isinstance(x[1], GeneratorExit):
raise
res = e.value
finally:
del x
finally:
del i, snd, throw
RESULT = res
del res

Maybe it's excessive to specify all the 'del's, but I'm thinking the
'with' statement might have destroyed or set to None the 'as <foo>'
variable today if the spec had taken care to specify deletes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top