[Python-ideas] Decorators on loops

Ned Batchelder ned at nedbatchelder.com
Wed Jan 8 13:17:36 CET 2014


On 1/8/14 6:21 AM, Enric Tejedor wrote:
> Hello,
>
> I would like to discuss a new use of python decorators. I apologize if
> this has already been suggested before.
>
> 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 the case of function and class decorators, Python has an object that 
can be passed to the decorator: the function or the class.  For a loop 
decorator, how would you "have access to the loop body"?  It sounds like 
it would have to be compiled differently, into a separate code object?

--Ned.
>
> In my case, I would use this to parallelize the iterations of the loop.
>
>
> Thank you for your feedback,
>
>
> Enric
>
> WARNING / LEGAL TEXT: This message is intended only for the use of the
> individual or entity to which it is addressed and may contain
> information which is privileged, confidential, proprietary, or exempt
> from disclosure under applicable law. If you are not the intended
> recipient or the person responsible for delivering the message to the
> intended recipient, you are strictly prohibited from disclosing,
> distributing, copying, or in any way using this message. If you have
> received this communication in error, please notify the sender and
> destroy and delete any copies you may have received.
>
> http://www.bsc.es/disclaimer
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



More information about the Python-ideas mailing list