making a variable available in a function from decorator

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Jul 30 12:02:32 EDT 2007


rkmr.em at gmail.com a écrit :
(top-post corrected)
> 
> On 30 Jul 2007 06:17:25 GMT, Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
>> On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em at gmail.com wrote:
>>
>>> I create a variable in a decorator. i want to be able to access that
>>> variable in the function to be decorated. How to do this?
 >
>> Pass it as argument to the function:
>>
>> def deco(func):
>>     eggs = 42
>>     def decorated(*args, **kwargs):
>>         kwargs['spam'] = eggs
>>         func(*args, **kwargs)
>>     return decorated
>>
>> @deco
>> def test(parrot, spam):
>>     print parrot, spam

 > is it possible to do this without passing it as a function argument?

What's your use case, exactly ? Having a function depending on a name 
being set by a decorator is not exactly pythonic, and arguments are 
meant to pass variables to functions...





More information about the Python-list mailing list