I have the following class in Python that I am trying to use for calling a set of its own methods through a dictionary that has the pointers of the available functions:
class Test():
functions = {
'operation_a' : Test.function_a;
'operation_b' : Test.function_b;
}
def run_operations(operation, *args, **kwargs):
try:
functions[str(operation)](self, args, kwargs)
except KeyError:
// some log ...
def function_a(self, *args, **kwargs):
print A
def function_b(self, *args, **kwargs):
print B
This first approach seems to be incorrect since the Python interpreter cannot find class 'Test' (NameError: 'Test' is not defined). I could not find a way (either importing the package, the package and the module, from package.module import *... etc.) Therefore, I have 3 solutions for this issue that already worked for me:
- define the operations dictionary within the class constructor (
__init__()), - move the callable functions to a different class (in my case, the class is in a different module, I did not try with a class within the same module),
- define within the same class the functions as @staticmethod
However, I still do not know why the initial approach does not seem to be correct. Therefore, how can I reference to a function within the same class before instantiation?