Static Variables
Peter Norvig
peter at norvig.com
Wed Apr 3 04:43:01 EST 2002
You can also use optional arguments to functions and methods. These
share with C static variables the property of being assigned once:
def f(x, s=initialization_code()):
# s behaves like a static variable
...
Peter Hansen <peter at engcorp.com> wrote in message news:<3CAA7FD0.56C285E0 at engcorp.com>...
> Jim Jacobs wrote:
> >
> > Is it possible to simulate "C" style static variables in functions? In
> > methods?
>
> Maybe ... what do you mean by simulate, and what characteristics
> of the C statics would you like to have?
>
> In C, static inside a function causes the variable to be assigned
> in the heap rather than on the stack (to put it one way) and
> that gives the variable persistence across invocations of
> the function (but scope local to the function).
>
> In Python, you would use either an instance variable, inside
> a method of an object, or the answer would be "no, but can
> you get by with using 'global'?".
>
> -Peter
More information about the Python-list
mailing list