By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

making a variable available in a function from decorator

P: n/a
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
Jul 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sun, 29 Jul 2007 15:22:47 -0700, rk*****@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

Ciao,
Marc 'BlackJack' Rintsch
Jul 30 '07 #2

P: n/a
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****@gmx.netwrote:
On Sun, 29 Jul 2007 15:22:47 -0700, rk*****@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

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list
Jul 30 '07 #3

P: n/a
rk*****@gmail.com a écrit :
(top-post corrected)
>
On 30 Jul 2007 06:17:25 GMT, Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
>On Sun, 29 Jul 2007 15:22:47 -0700, rk*****@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...
Jul 30 '07 #4

P: n/a
On 7/30/07, rk*****@gmail.com <rk*****@gmail.comwrote:
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 <ev**@yelp.com>
Jul 30 '07 #5

P: n/a
Evan Klitzke a écrit :
On 7/30/07, rk*****@gmail.com <rk*****@gmail.comwrote:
>>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()
>>def foo():
.... print foo.c
....
>>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
>>>
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.
Jul 31 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.