Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 29th, 2007, 11:25 PM
rkmr.em@gmail.com
Guest
 
Posts: n/a
Default 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
  #2  
Old July 30th, 2007, 07:25 AM
Marc 'BlackJack' Rintsch
Guest
 
Posts: n/a
Default 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
  #3  
Old July 30th, 2007, 03:45 PM
rkmr.em@gmail.com
Guest
 
Posts: n/a
Default 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
>
  #4  
Old July 30th, 2007, 05:05 PM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default 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...


  #5  
Old July 30th, 2007, 06:05 PM
Evan Klitzke
Guest
 
Posts: n/a
Default 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>
  #6  
Old July 31st, 2007, 02:25 AM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default 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()
Quote:
Quote:
Quote:
>>def 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:
Quote:
Quote:
>>>
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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles