Newbie asks about static variables...

stephen.allison at cenes.co.uk stephen.allison at cenes.co.uk
Tue May 4 13:04:09 EDT 1999


Hello all,

I've just recently started learning Python and am basically very impressed.
One thing I can't figure out is how to do static variables though.  So, for
example how could you do the following C code in Python:

void killParrot()
{
    static int no_of_calls = 0;

    // do stuff

    printf("Number of calls is %d", no_of_calls);
    no_of_calls ++;
}
and elsewhere...

killParrot();
killParrot();

which would give (of course):
Number of calls is 1
Number of calls is 2

If i try to do copy the above 'directly' into python no_of_calls is reset each
time the function is called (which is fair enough), and I can only acjieve the
required behaviour by having decaling number_of_calls as 'global', which
doesn't seem very elegant.  It seems a funny thing not to be in a language
which is other wise very nice.  Perhaps I'm not 'thinking in python' yet, can
anyone shed some light?

TIA

Steve



--
Steve Allison
Never reads dejanews mail

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




More information about the Python-list mailing list