[Python-ideas] Anonymous blocks (again):

Nick Coghlan ncoghlan at gmail.com
Sun May 12 07:15:57 CEST 2013


On Sun, May 12, 2013 at 2:10 PM, Juancarlo Añez <apalala at gmail.com> wrote:
> Hello,
>
> I've banged my self against the wall, and there simply isn't a pythonic way
> to make a context manager iterate over the yielded-to block of code.
>
> I've had to resource to this in Grako[*]:
>
>         def block():
>             self.rule()
>             self.ast['rules'] = self.last_node
>         closure(block)

In current Python, decorator abuse can be a reasonable option:

   @closure
   def block():
        self.rule()
        self.ast['rules'] = self.last_node

Or, if PEP 403 were accepted and implemented:

    @in closure(f):
    def f():
        self.rule()
        self.ast['rules'] = self.last_node

(The latter would have the advantage of working with arbitrary expressions)

Anyway, if this is a topic that interests you, I strongly recommend
reading both PEP 403 and PEP 3150 in full.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list