Static Variables in Python?

Michael Yanowitz m.yanowitz at kearfott.com
Tue Aug 1 07:37:20 EDT 2006



-----Original Message-----
From: Cliff Wells [mailto:cliff at develix.com]
Sent: Monday, July 31, 2006 4:55 PM
To: Michael Yanowitz
Cc: python-list at python.org
Subject: Re: Static Variables in Python?


On Mon, 2006-07-31 at 15:21 -0400, Michael Yanowitz wrote:
>   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.

BTW, I'm assuming this example was contrived.  In real life, I wonder
why you'd ever want to use anything besides:

bits = [ 0 ] * 16
bits [ 4 ] = 1
print "Bit Array:"
print ' '.join ( bits )

Having a "set_bit" function seems redundant when the language syntax
directly supports what you are trying to do.

Regards,
Cliff

-- 

   Thanks everyone for your help. Yes I know it is contrived. Well it is
as over-simplified version of what I really want. And yes, I do realize
after sending it about the infinite loop in the printing. I tried too
quickly to come up with a good example without testing it first.
   I like the class idea, however I realize that the class object itself
has to be global. I will look into the decorators - something which I have
avoided until now.
   I tried creating a class, but got an error: 

# ********* class BitsClass *****************************************  
class BitsClass (object):
    def __init__(self, num_bits):
        self.bits=[]
        for i in range(num_bits):
            self.bits.append(0)
    def set(self, bit_index, value):
		self.bits[bit_index] = value
		return self.bits
    def get(self, bit_index):
        if ((bit_index >= 0) and (bit_index < len(self.bits))):
            return self.bits[bit_index]
        else:
            return scenario_globals.ERROR_
    def display(self):
        i = 0
        while (i < len(self.bits)):
            print self.bits[i],
            i += 1
        print '\n',
        
global the_bits
the_bits = BitsClass(16)

# inside another function I have:
    global the_bits
    the_bits.set(index, value)

  but I get back:
Traceback (most recent call last):
   ...
  File "scenario_sync.py", line 245, in get_discrete_data
    the_bits.set(index, value)
AttributeError: 'DiscreteBits' object has no attribute 'set'

  There is 

  I was also disappointed, I was hoping I could use BitsClass.print()
instead of BitsClass.display().

Thanks in advance:
 Michael Yanowitz





More information about the Python-list mailing list