Function to execute only once

Jaime Wyant programmer.py at gmail.com
Fri Oct 14 15:23:49 EDT 2005


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
global value of `tmp', which is 0, and adds it to the *local* tmp.

I hope that is not too confusing.

jw

On 14 Oct 2005 12:11:58 -0700, PyPK <superprad at gmail.com> 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 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.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list