Newbie: anything resembling static?

Mongryong Mongryong at sympatico.ca
Wed Jan 29 17:48:01 EST 2003


One, if not, the only reason, for having a static variable in a function
is if you wanted to conserve 'global' memory space.  For example, if the
function with the static variable is never called in your application,
'global' memory is not wastefully allocated for that variable.  Event
callback functions are great examples of when to use static function
variables.

If you're just concern about namespace issues, you're much better off to
use a global variable with a unique naming scheme.

But if you're reasons for wanting a static function variable is to
conserve wasting 'global' memory (as I've described above), you're
better to use a approach like this:

__callback_static_var = None
def callback(x):
	if __callback_static_var is None:
		__callback_static_var = CreateSomeStaticVar()

	...process

In fact, this is similar to what C/C++ do in their implementation of
static function variables.  The only difference being that in Python the
object is always created on 'heap'.

Hope that helps :)







More information about the Python-list mailing list