static variables?

Courageous jkraska at san.rr.com
Mon Nov 18 23:45:07 EST 2002


>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?

Usually this is done as a member on the Class and not on in the Instance.
Like this:

>>> class MyClass:
	myStatic=0
	def f(self):
		MyClass.myStatic+=1
		print `MyClass.myStatic`

		
>>> c = MyClass()
>>> c.f()
1
>>> c.f()
2
>>> 



More information about the Python-list mailing list