471,357 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

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
Jul 29 '07 #1
5 5364
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
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
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
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
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.

Similar topics

4 posts views Thread by fortepianissimo | last post: by
5 posts views Thread by Greg Swindle | last post: by
134 posts views Thread by James A. Donald | last post: by
1 post views Thread by Grzegorz Smith | last post: by
pbmods
1 post views Thread by pbmods | last post: by
11 posts views Thread by =?UTF-8?Q?Filip_Gruszczy=C5=84ski?= | last post: by
8 posts views Thread by Viktor | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.