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 -- -- http://mail.python.org/mailman/listinfo/python-list