Functionality like local static in C

Schachner, Joseph Joseph.Schachner at Teledyne.com
Thu Apr 14 14:29:07 EDT 2022


Yes, python has something like that.  In fact, two things.   

1) Generator.  Use a "yield" statement.   Every call "yields" a new value.   The state of the function (local variables) is remembered from each previous call to the next.

2) In a file, declare a variable to be global.   In the function declare global var, so that it will not only read the global but will also write it.  That variable does not go away. On the next time the function is called, It will hold whatever value it had when the function finished previously.

---- Joseph S.


Teledyne Confidential; Commercially Sensitive Business Data

-----Original Message-----
From: Cecil Westerhof <Cecil at decebal.nl> 
Sent: Thursday, April 14, 2022 11:02 AM
To: python-list at python.org
Subject: Functionality like local static in C

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


More information about the Python-list mailing list