[Python-ideas] Anonymous blocks (again):

Juancarlo Añez apalala at gmail.com
Mon May 13 17:40:49 CEST 2013


On Mon, May 13, 2013 at 10:39 AM, Juancarlo Añez <apalala at gmail.com> wrote:

> If you want something different to this, then I think you do want new
>> syntax. Otherwise, what do you gain beyond what can already be done now?
>>
>> Or am I missing something?
>>
>
> It's not obvious from my example, but the idea is that the invoker be able
> to provide context for _each_ iteration.
>

I can explain better. While parsing a closure, the parser knows it should
stop iterating because the embedded expression fails to parse midway. At
that point, the last iteration must be rolled back (rewind the input stream
and discard syntax tree nodes). To roll back just the last iteration, the
iterator needs to control the context for each iteration, because it can't
predict which will be the last, the one to fail.

The above works perfectly defining a function for the embedded expression
using a synthetic name, like "c123", and passing it to closure():

def c123():
    match_this()
    match_that()

closure(c123)


My quest is because the above seems quite unpythonic.

Lakshmi suggested this pattern, which I think is an improvement:

with closure() as c:
    @c.exp
    def expre():
        match_this()
        match_that()


What I think would be great is to have the action (closure) precede an
anonymous block.

closure(
    def():
        match_this()
        match_that()
)


Cheers,

-- 
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130513/8cf4c991/attachment.html>


More information about the Python-ideas mailing list