Equiv of C's static local vars in Py ?

Az Tech aztech1200 at yahoo.com
Sun Dec 1 15:10:22 EST 2002


Hi group,

Platform: Active Python 2.2.1 on Win 98.

I've done a recursive grep on the Py docs dir for
"local static", also Google'd for it, didn't find
anything that seemed relevant.

What, if any, is the equivalent of C's static local 
variables in Python ? E.g. if I have a C function :

void f(void)
{
  static int call_count = 0;
  call_count++;
  printf("f() has been called %d times\n", call_count);
  /* 
    other code
  */
}

This function f retains the value of call_count between successive 
calls to it - so each time it is called, the value for call_count
printed out will be one more than the last one.

Can this be done in Python and if so, how ?

P.S. I know this can be done using OO in Py (by declaring a class that
contains a member/field and then instantiating objects of that class);
but I have an app that I want to first develop in a non-OO 
(procedural) style, and only then convert it to OO style 
(for the purpose of learning/exploring language Python features).

P.P.S. I actually don't want to use the feature for maintaining
a call count, but for some other purpose, but the principle is the same.

TIA
Az (not my real name - use it for tech queries becoz of spam).



More information about the Python-list mailing list