Functionality like local static in C

Barry barry at barrys-emacs.org
Thu Apr 14 12:11:45 EDT 2022



> On 14 Apr 2022, at 16:28, Cecil Westerhof via Python-list <python-list at python.org> 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?

You can define variables at the module level and then use global to use
them in your function.

a_static_var = 42

def func(value):
    global a_static_var
    a_static_var = value

Barry

> 
> -- 
> Cecil Westerhof
> Senior Software Engineer
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 


More information about the Python-list mailing list