static variables?

Erik Max Francis max at alcyone.com
Mon Nov 18 23:26:56 EST 2002


Josh wrote:

> I am a python newbie, and I am sure there is an easy answer but, Is
> there
> any equivalent to C's static variables in Python? If not, how can you
> have
> variables inside a function, that 'remember' their values between
> function
> calls?

Typically one would just have a global (module scope) variable with a
leading underscore (a common convention meaning, "Don't touch me unless
you're part of the implementation."

	_count = 0
	def call():
	    global _count
	    _count += 1
	    print "Count is %d" % _count

In class, you can get the effect of "static variables" by defining
variables in class scope, not local scope.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Pick the roses from the thorns / Wash with waters of the storms
\__/ Chante Moore
    PyUID / http://www.alcyone.com/pyos/uid/
 A module for generating "unique" IDs in Python.



More information about the Python-list mailing list