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 5 5488
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
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 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...
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>
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fortepianissimo |
last post by:
We all know that using __getattr__() we can compute an instance
variable on demand, for example:
class Foo:
def __getattr__ (self, name):
if name == 'bar':
self.bar = 'apple'
return self.bar...
|
by: Greg Swindle |
last post by:
Hello,
I have a question about how prototyping relates to variables and their
scope.
Given the following code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var ParentObject = function()
{...
|
by: James A. Donald |
last post by:
I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read
"no variable or argument declarations are necessary."
Surely that...
|
by: Grzegorz Smith |
last post by:
Hi all. I have got situation that I need make parameter injection into
function and I want that parametet be a local variable of that function.
eg. something like
def decorator():
<code>...
|
by: prouleau001 |
last post by:
Hi all!
Since that the decorator syntax is upon us, I think it would be good if
atexit.register() was returning the function passed as argument. This
simple change to the library would solve a...
|
by: pbmods |
last post by:
VARIABLE SCOPE IN JAVASCRIPT
LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN )
PREREQS: VARIABLES
First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
|
by: =?UTF-8?Q?Filip_Gruszczy=C5=84ski?= |
last post by:
Hello everyone!
It is my first message on this list, therefore I would like to say
hello to everyone. I am fourth year student of CS on the Univeristy of
Warsaw and recently I have become very...
|
by: Viktor |
last post by:
Can somebody give me an explanation what happened here (or point me to
some docs)?
Code:
HMMM = None
def w(fn):
print 'fn:', id(fn)
HMMM = fn
|
by: Rafe |
last post by:
Hi,
I'm working within an application (making a lot of wrappers), but the
application is not case sensitive. For example, Typing obj.name,
obj.Name, or even object.naMe is all fine (as far as...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |