[Python-ideas] Decorators on loops

Chris Angelico rosuav at gmail.com
Wed Jan 8 12:59:10 CET 2014


On Wed, Jan 8, 2014 at 10:21 PM, Enric Tejedor <enric.tejedor at bsc.es> wrote:
> The basic idea would be to support decorators on loops, in addition to
> functions and classes. Something like this:
>
> @mydecorator
> for i in range(10):
>      # loop body
>
> In mydecorator, I would like to have access to the loop body and the
> iterable object.
>
> In my case, I would use this to parallelize the iterations of the loop.

That's a nice theory, but the basic form of the decorator wouldn't
work. Here's how decorators work on functions:

@foo
def bar():
    pass

is the same as:

def bar():
    pass
bar = foo(bar)

It depends on there being something assigned-to. With loops, that's
not the case, so it's not possible to decorate them in the usual
sense.

Can you turn your loop into a map() call? Something like this:

def loop_body(i):
    # all the code for your loop body
list(map(loop_body, range(10)))

Once you have it in that form, you can use multiprocessing.Pool() and
its map() method, which will parallelize the loop for you (by
distributing it over a pool of subprocesses). Would that cover what
you need?

ChrisA


More information about the Python-ideas mailing list