| | |
static variables in python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2005
Posts: 18
Reputation:
Solved Threads: 0
Hello fellows...
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:
int static_num()
{
static int x = 0;
return ++x;
}
void main()
{
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?
thanks a lot!
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:
int static_num()
{
static int x = 0;
return ++x;
}
void main()
{
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?
thanks a lot!
Python Syntax (Toggle Plain Text)
x=0 def static_num() : global x x=x+1 return x for i in range(0,10) : print static_num
Bumsfeld you are right, avoid globals, they do lead to errors. Using a generator function avoids globals ...
python Syntax (Toggle Plain Text)
def static_num2(): k = 0 while True: k += 1 yield k static = static_num2().next for i in range(0,10) : print static()
Last edited by vegaseat; Dec 3rd, 2007 at 1:28 am. Reason: changed php tags
May 'the Google' be with you!
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Solved Threads: 2
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:
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
The code for a class would use a class method for flair.
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.
Tom
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:
python Syntax (Toggle Plain Text)
def last_argument(n, arg_store = []): "Return the argument used when the function was last called" arg_store.append(n) if len(arg_store) == 1: return -1 else: return arg_store[-2]
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
python Syntax (Toggle Plain Text)
state = [] last_argument(1, state) last_argument(2, state)
The code for a class would use a class method for flair.
python Syntax (Toggle Plain Text)
class A(object): last = -1 @classmethod def last_argument(cls, n): temp = cls.last cls.last = n return temp A.last_argument(1) A.last_argument(17)...
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.
Tom
Last edited by vegaseat; Dec 3rd, 2007 at 1:34 am. Reason: changed code tags from [\code] to [/code]
![]() |
Similar Threads
- How do I go about viewing static variables while debugging? (C++)
- Static Variables: (C++)
- Comparing Python and C, Part 1, Integer Variables (Python)
- static variables?? (MySQL)
Other Threads in the Python Forum
- Previous Thread: python calulator help
- Next Thread: wxpython frame clear/refresh
Views: 48231 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
anti assignment avogadro beginner bluetooth code convert count csv decimals def dictionary dynamic dynamically enter examples excel file float format frange ftp function gnu gui heads homework import input jaunty java lapse leftmouse line lines linux list lists loop microcontroller module mouse multiple newb number numbers output parsing path pointer port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random raw_input recursion recursive redirect scrolledtext slicenotation software sqlite ssh stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape windows word wx.wizard wxpython






