|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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. |
#2
| |||
| |||
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. |
#3
| |||
| |||
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() |
![]() |
|
Tags: method, python, static variable |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |