I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.
Here's some sample code:
class a:
lut = [1,
3,
17,
[12,34],
5]
Where lut is static, and is expected to be constant as well.
and now I wan to do the following:
class a:
def spam0(self):
return (some_calculation_based_on_self)
def spam1(self):
return (a_different_calculation_based_on_self)
lut = [1,
3,
17,
[12,34],
5,
self.spam0
self.spam1]
This doesn't compile because self.spam0 and self.spam1 are undefined. I tried using a.spam but that is also undefined. How can I set lut[5] to return a reference to self.spam?
Edit: This is what I plan to do:
(continuing the definition of class a):
import inspect
# continue to somewhere in the definition of class a
def __init__(self, param):
self.param = param
def eggs(self):
tmp = lut[param]
if (insect.isfunction(tmp)): # if tmp is self.spam()
return tmp() # should call it here
return tmp
So I want to either return a simple value or run some extra code, depending on the parameter.
Edit: lut doesn't have to be a class property, but the methods spam0 and spam1 do need to access the class members, so they have to belong to the class.
I'm not sure that this is the best way to do this. I'm still in the process of working this out.
__init__and the final value computed. The "eggs" method can be a simple property that returns a value computed by__init__. Your design must be incomplete; please provide a more complete code snippet.