I've seen people use function attributes for this. ``` Python 3.10.2 (main, Jan 15 2022, 19:56:27) [GCC 11.1.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def function(): ... print(function.variable) ... function.variable += 1 ... >>> function.variable = 1 >>> function() 1 >>> function() 2 >>> ```
If necessary, the variable can be initialised inside the function too. Kind Regards, Sam Ezeh On Thu, 14 Apr 2022 at 16:36, Sam Ezeh <sam.z.e...@gmail.com> wrote: > > I've seen people use function attributes for this. > ``` > Python 3.10.2 (main, Jan 15 2022, 19:56:27) [GCC 11.1.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> def function(): > ... print(function.variable) > ... function.variable += 1 > ... > >>> function.variable = 1 > >>> function() > 1 > >>> function() > 2 > >>> > ``` > > If necessary, the variable can be initialised inside the function too. > > Kind Regards, > Sam Ezeh > > > On Thu, 14 Apr 2022 at 16:26, Cecil Westerhof via Python-list > <python-list@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? > > > > -- > > Cecil Westerhof > > Senior Software Engineer > > LinkedIn: http://www.linkedin.com/in/cecilwesterhof > > -- > > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list