On 14 Oct 2005 12:11:58 -0700, PyPK [EMAIL PROTECTED] wrote:
Hi if I have a function called
tmp=0
def execute():
tmp = tmp+1
return tmp
also I have
def func1():
execute()
and
def func2():
execute()
now I want execute() function to get executed only once. That
Bengt Richter wrote:
snip
tmp = 0
def execute():
... global tmp, execute
... tmp = cellvar = tmp + 1
... def execute():
... return cellvar
... return tmp
snip
On man did this put my head into a spin :P
--
Lasse Vågsæther Karlsen
Hi if I have a function called
tmp=0
def execute():
tmp = tmp+1
return tmp
also I have
def func1():
execute()
and
def func2():
execute()
now I want execute() function to get executed only once. That is the
first time it is accessed.
so taht when funcc2 access
If I understand you correctly, you want `tmp' to be global...
If so, declare it as so in execute -
def execute():
global tmp
tmp = tmp+1
return tmp
Otherwise, what happens is that you declare a variable local to
execute, that is named tmp. When the assignment occurs it uses the
PyPK [EMAIL PROTECTED] writes:
now I want execute() function to get executed only once. That is the
first time it is accessed.
so taht when funcc2 access the execute fn it should have same values as
when it is called from func1.
There's nothing built into Python for that. You have to program
PyPK wrote:
now I want execute() function to get executed only once. That is the
first time it is accessed.
How about just calculating the value at import time?
--
Benji York
--
http://mail.python.org/mailman/listinfo/python-list
I've been seeing alot about decorators and closures lately and my
initial thought was that this would be a good place to use them instead
of wrapping it around a class. That was my initial thought :) What I
came up with was this:
def execute_once(fn):
result = None
def executor(*args,
The problem seemed to be because I was rebinding result inside
executor. Can someone explain why it works below but not in the first
one?
Also why is it if I set tmp as a global and don't pass it as a
paremeter to the various functions as per the OP that I get an
UnboundLocalError: local variable
snoe [EMAIL PROTECTED] writes:
Also why is it if I set tmp as a global and don't pass it as a
paremeter to the various functions as per the OP that I get an
UnboundLocalError: local variable 'tmp' referenced before assignment?
If you don't declare it as a global, and if you try to assign a
snoe [EMAIL PROTECTED] wrote:
I've been seeing alot about decorators and closures lately and my
initial thought was that this would be a good place to use them instead
of wrapping it around a class. That was my initial thought :) What I
came up with was this:
Apparently you're not the first
10 matches
Mail list logo