Static Variables in Python?

Michael Yanowitz m.yanowitz at kearfott.com
Mon Jul 31 15:21:23 EDT 2006


  Is it possible to have a static variable in Python - 
a local variable in a function that retains its value.

 For example, suppose I have:

def set_bit (bit_index, bit_value):
   static bits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
   bits [bit_index] = bit_value

   print "\tBit Array:"
   int i
   while (i < len(bits):
      print bits[i],
   print '\n'

 
   I realize this can be implemented by making bits global, but can
this be done by making it private only internal to set_bit()?  I don't
want bits to be reinitialized each time. It must retain the set values
for the next time it is called.


Thanks in advance:
Michael Yanowitz






More information about the Python-list mailing list