Functionality like local static in C

Joe Pfeiffer pfeiffer at cs.nmsu.edu
Thu Apr 14 12:57:01 EDT 2022


Cecil Westerhof <Cecil at decebal.nl> writes:

> 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?

Others (in particular mirko) have given ways to emulate that
functionality.

I'll mention that local statics are frequently not found in object
oriented languages because member variables can serve much the same
function in a more general way.  If you think you need a static local
variable, ask yourself if what you really need is a class.


More information about the Python-list mailing list