Is there something like static function variables in python?
I know there are static attributes of a class, but what if i want
to use a static variable inside a function?
somthing like this (stupid) code in c++ for example:
static int x = 0;
for (int i=0; i<10; i++)
cout << static_num() << endl;
If there aren't such static variables , is there a way for doing something similar to this c++ code?
Hmm. There are a couple of ways you could simulate the effect of static variables - but they aren't explicitly in the language.
One way is to abuse the fact that default parameters for functions are evaluated only once when the function is defined, and then kept for the rest of time - so that if one makes the default value a new list or dictionary then it can be used to share values between any running version of the function. For example:
def last_argument(n, arg_store = ):
"Return the argument used when the function was last called"
if len(arg_store) == 1:
I'm not sure whether this would be classed as a 'good solution' or not... I don't know whether it counts as confusing to use stored default arguments for sharing state - also you are never going to use this parameter.
As an alternative you could explicit give the function somewhere to store its state each time it's called. We could do this using an explicit paramter, a class or an instance.
For a parameter we'd effectively use the same as above, apart from we wouldn't specify a default value. We'd then call
state = 
The code for a class would use a class method for flair.
last = -1
def last_argument(cls, n):
temp = cls.last
cls.last = n
The code an instance of a class is much as you would expect.
The default argument approach is probably the approach closest to a static variable, the class method might feel the 'cleanest' - depending on one's bigotries.
For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
For Each subCtrl As Control In ctrl.Controls
If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
If subCtrl.GetType Is ...