TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Is it possible to have static variable in python? (http://forums.techarena.in/software-development/1217802.htm)

gvBlake 21-07-2009 09:45 PM

Is it possible to have static variable in python?
 
Hi,

This is not a perfect question from me since I am not much experienced with python.
I have a simple question to ask, is it possible in python to have a static variable or a method just like C++? If yes can anyone please help me with an example here?

Thanks in advance.

migons 21-07-2009 09:53 PM

Re: Is it possible to have static variable in python?
 
Well to say particularly for python to have static variable its not there as far as I know.
But you can definitely do the same in various ways for python like for C.
Just use global variable. you can declare bits in set_bit() as "global bits = ..." this will create a global variable & you don't need to declare it outside the function. You have to take care with the name you use while declaration that it wont be confusing.
You can call set_bit(..) as a function.

beatnik_abx 21-07-2009 09:56 PM

Re: Is it possible to have static variable in python?
 
In my opinion one must avoid using global variables since it leads to errors.
You can use a generator function instead.

Code:

def static_num2():

a = 0

while True:

a += 1

yield a

 
static = static_num2().next

 
for j in range(0,10) :

print static()



All times are GMT +5.5. The time now is 10:01 PM.