Re: Function to execute only once

2005-10-16 Thread Bengt Richter
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

Re: Function to execute only once

2005-10-16 Thread Lasse Vågsæther Karlsen
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

Function to execute only once

2005-10-14 Thread PyPK
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

Re: Function to execute only once

2005-10-14 Thread Jaime Wyant
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

Re: Function to execute only once

2005-10-14 Thread Paul Rubin
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

Re: Function to execute only once

2005-10-14 Thread Benji York
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

Re: Function to execute only once

2005-10-14 Thread snoe
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,

Re: Function to execute only once

2005-10-14 Thread snoe
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

Re: Function to execute only once

2005-10-14 Thread Paul Rubin
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

Re: Function to execute only once

2005-10-14 Thread George Sakkis
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