Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-07-2009
Member
 
Join Date: Apr 2008
Posts: 49
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.
Reply With Quote
  #2  
Old 21-07-2009
Member
 
Join Date: Apr 2008
Posts: 49
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.
Reply With Quote
  #3  
Old 21-07-2009
Member
 
Join Date: May 2008
Posts: 35
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()
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Is it possible to have static variable in python?"
Thread Thread Starter Forum Replies Last Post
Servlet static variable Sheenas Software Development 5 06-03-2010 11:24 AM
Static variable initialization Dilbert Software Development 3 31-12-2009 01:38 PM
Runtime Error 91 : Object Variable or with block variable not set Ryan21 Software Development 2 28-08-2009 07:51 PM
What is Global Variable static? Declaring static variable instead of Global variable Reckon Software Development 3 25-04-2009 11:34 AM
Can we use static variable in other programming languages FlayoFish Software Development 2 25-04-2009 10:56 AM


All times are GMT +5.5. The time now is 08:58 PM.