Results 1 to 3 of 3

Thread: Is it possible to have static variable in python?

  1. #1
    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.

  2. #2
    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.

  3. #3
    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()

Similar Threads

  1. Servlet static variable
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 11:24 AM
  2. Static variable initialization
    By Dilbert in forum Software Development
    Replies: 3
    Last Post: 31-12-2009, 01:38 PM
  3. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  4. Replies: 3
    Last Post: 25-04-2009, 11:34 AM
  5. Can we use static variable in other programming languages
    By FlayoFish in forum Software Development
    Replies: 2
    Last Post: 25-04-2009, 10:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,551,713.96502 seconds with 17 queries