[Python-ideas] Decorators on loops

Masklinn masklinn at masklinn.net
Wed Jan 8 13:08:31 CET 2014


On 2014-01-08, at 12:59 , Chris Angelico <rosuav at gmail.com> wrote:

> 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?

Alternatively, wrap the loop in a function and then do AST munging in
the decorator. Something similar (in spirit at least) to Numba
(http://numba.pydata.org).

You could even do something like immediate function invocation in the
decorator, and bind the result to the function name, although I'm not
sure your coworkers will like you.


More information about the Python-ideas mailing list