[Tutor] C-like local static var in Py ?

Aztech Guy aztech1200@yahoo.com
Sun Dec 1 17:43:01 2002


--0-456869479-1038782539=:30786
Content-Type: text/plain; charset=us-ascii


 

Hi,

(Checked docs, couldn't find relevant info)

How can I get the effect of a "local static" variable as in C - in Python ?

e.g. with this C function  ...

void f(void)

{

    static int call_count = 0;

    call_count++;

    printf("call_count = %d\n", call_count);

    /* other code */

}

... each time I call f(), the value of call_count is incremented by 1 over the previous value it had when the function was last exited, unlike an "automatic" local variable (which it would be if I omitted the "static" keyword - and which would get reset to 0 on each call to f() ). I know this can be done by using OO, i.e. a class, and retaining state with a member variable, but I want to see if it can be done in non-OO, procedural code.

 

TIA

Az

 



---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
--0-456869479-1038782539=:30786
Content-Type: text/html; charset=us-ascii

<P>&nbsp;</P>
<P>Hi,</P>
<P>(Checked docs, couldn't find relevant info)</P>
<P>How&nbsp;can I&nbsp;get the effect of a "local static" variable as in C - in Python ?</P>
<P>e.g. with this C function&nbsp; ...</P>
<P>void f(void)</P>
<P>{</P>
<P>&nbsp;&nbsp;&nbsp; static int call_count = 0;</P>
<P>&nbsp;&nbsp;&nbsp; call_count++;</P>
<P>&nbsp;&nbsp;&nbsp; printf("call_count = %d\n", call_count);</P>
<P>&nbsp;&nbsp;&nbsp; /* other code */</P>
<P>}</P>
<P>... each time I call f(), the value of call_count is incremented by 1&nbsp;over&nbsp;the previous value&nbsp;it had when the function was last exited, unlike an "automatic" local variable (which it would be if I omitted the "static" keyword - and which would get reset to 0 on each call to f() ). I know this can be done by using OO, i.e. a class, and retaining state with a member variable, but I want to see if it can be done in non-OO, procedural code.</P>
<P>&nbsp;</P>
<P>TIA</P>
<P>Az</P>
<P>&nbsp;</P><p><br><hr size=1>Do you Yahoo!?<br>
<a href="http://rd.yahoo.com/mail/mailsig/*http://mailplus.yahoo.com">Yahoo! Mail Plus</a> - Powerful. Affordable. <a href="http://rd.yahoo.com/mail/mailsig/*http://mailplus.yahoo.com">Sign up now</a>
--0-456869479-1038782539=:30786--