[Python-ideas] adding an __exec__ method to context managers?

Paul Moore p.f.moore at gmail.com
Wed Oct 14 11:52:40 CEST 2009


2009/10/13 Bruce Leban <bruce at leapyear.org>:
> It seems to me that the right way to support this kind of thing is
> decorators on suites:
> def sample_for():
>     @parallel.for:
>         for i in range(n):
>             do_something(i)

You don't need decorators on suites - at a minimum you can use a
defined function (possibly with a throwaway name like _):

def sample_for():
    @parallel.for
    def _():
        for i in range(n):
            do_something(i)

You just need to modify parallel.for to call the newly defined
function as follows (pseudocode):

def parallel.for(fn):
    ... old body of parallel.for
    ... assume it returns a function inner
    ... instead of "return inner", do
    inner()
    return None

Summary: you don't need decorators on bare blocks, just name the block.

(OK, scope issues may impact this, but you didn't define how scope is
handled in a "decorated block" in any case, so I choose to assume it
introduces a new scope just like def does :-))

Paul.



More information about the Python-ideas mailing list