[Python-ideas] Extended "break" "continue" in "for ... in" block.

海韵 lyricconch at gmail.com
Thu May 24 12:47:48 CEST 2012


Hi all...
i'd like to propose a syntax extend of "break" and  "continue"
to let them work together with "yield".

Syntax:
continue_stmt: "continue" [ test ]
break_stmt: "break" [ test ]
it is only valid in "for ... in" block.

when we writing "for <fields> in <container>: <suite>",
we can say there is a generator ("__g = iter(<container>)") providing values
("<fields> = next(__g)") and the values are processing by <suite>.
<suite> implenetments computing. (we focus)
inside geneator("__iter__" of <container>) implenetments iteration.
(sealed logic)
---- as current ----
"continue" is "next(__g)" (which equals to "__g.send(None)"),
"break" leave the block and __g is garbage collected(which implies a
__g.close()).
"return" "raise" inside <suite> leave the block and __g is garbage collected.

let's make thing reverse. consider we are write __g' code.
generator function implenetments computing. (we focus)
outside code(<suite> of "for ... in") implenetments continuation. (sealed logic)
---- as proposal ----
"continue <test>" is equiv to "__g.send(<test>)".
"continue" is alias of "continue None".
"break <test>" is equiv to "__g.throw(<test>)".
"break" is alias of "break GeneratorExit".
"return" "raise" inside <suite> impies a "break" to __g,

with communication between "yield" and "continue", "break",
this plays just as Ruby's block except return value of __g lost
(may we use an "as" after "for ... in" to fetch return value?).


-- 
= =!



More information about the Python-ideas mailing list