Is it possible to have static variables within class functions(as in C++).
The following does not give me what is desired.
The motivation is to initialize(very expensive process) a lookup list inside a function - but only if and when it is called.
Subsequent invocations of the same function should not need the variable to be initialized again.
Is there an idiom to achieve this?
Its fine if the function is aligned to the class;so that the value of rules is then available to all instances of 'A'
>>> class A:
... def func(self):
... if not hasattr(self.func,"rules"):
... print 'initialize'
... rules=[8,7,6]
... rules.append(4)
... return rules
...
>>> a=A()
>>> for x in range(5):
... print a.func()
...
initialize
[8, 7, 6, 4]
initialize
[8, 7, 6, 4]
initialize
[8, 7, 6, 4]
initialize
[8, 7, 6, 4]
initialize
[8, 7, 6, 4]