Functionality like local static in C

Chris Angelico rosuav at gmail.com
Fri Apr 15 15:13:54 EDT 2022


On Sat, 16 Apr 2022 at 04:51, Python <python at example.invalid> wrote:
>
> Cecil Westerhof wrote:
> > In C when you declare a variable static in a function, the variable
> > retains its value between function calls.
> > The first time the function is called it has the default value (0 for
> > an int).
> > But when the function changes the value in a call (for example to 43),
> > the next time the function is called the variable does not have the
> > default value, but the value it had when the function returned.
> > Does python has something like that?
>
> Sort of, one way is to have a argument with a default value
> which is mutable. Hint: don't do that.
>

Why do you anonymously suggest a thing and also suggest not doing it?
The default argument technique has been mentioned already as a
perfectly valid way to do this.

ChrisA


More information about the Python-list mailing list