
July 29th, 2007, 11:25 PM
| | | making a variable available in a function from decorator
Hi
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?
thanks | 
July 30th, 2007, 07:25 AM
| | | Re: making a variable available in a function from decorator
On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em@gmail.com wrote: Quote:
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
Ciao,
Marc 'BlackJack' Rintsch | 
July 30th, 2007, 03:45 PM
| | | Re: making a variable available in a function from decorator
is it possible to do this without passing it as a function argument?
On 30 Jul 2007 06:17:25 GMT, Marc 'BlackJack' Rintsch <bj_666@gmx.netwrote: Quote:
On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em@gmail.com wrote:
> Quote:
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
>
Ciao,
Marc 'BlackJack' Rintsch
-- http://mail.python.org/mailman/listinfo/python-list
>
| | 
July 30th, 2007, 05:05 PM
| | | Re: making a variable available in a function from decorator rkmr.em@gmail.com a écrit :
(top-post corrected) Quote:
>
On 30 Jul 2007 06:17:25 GMT, Marc 'BlackJack' Rintsch <bj_666@gmx.netwrote: Quote:
>On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em@gmail.com wrote:
>> Quote:
>>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?
| | > Quote:
>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
| | Quote: |
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... | 
July 30th, 2007, 06:05 PM
| | | Re: making a variable available in a function from decorator
On 7/30/07, rkmr.em@gmail.com <rkmr.em@gmail.comwrote: Quote:
is it possible to do this without passing it as a function argument?
>
| Sort of. Functions are objects in python, so you can set attribute on them. E.g.
def foo():
return foo.c
foo.c = 1
print foo()
Which will print 1. Of course, it would generally be better to write
your own class for this sort of thing, so that you can set the
variable in the instance scope.
--
Evan Klitzke <evan@yelp.com> | 
July 31st, 2007, 02:25 AM
| | | Re: making a variable available in a function from decorator
Evan Klitzke a écrit : Quote:
On 7/30/07, rkmr.em@gmail.com <rkmr.em@gmail.comwrote:
> Quote:
>>is it possible to do this without passing it as a function argument?
>>
| Sort of. Functions are objects in python, so you can set attribute on them. E.g.
>
def foo():
return foo.c
>
foo.c = 1
print foo()
| .... print foo.c
.... Quote: Quote: Quote:
>>foo.c = 1
>>bar = foo
>>del foo
>>bar()
| | | Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
NameError: global name 'foo' is not defined Quote:
Which will print 1. Of course, it would generally be better to write
your own class for this sort of thing, so that you can set the
variable in the instance scope.
| Indeed. But even with OO, explicit is better than implicit. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|