I'm writing some code and I have a dictionary where the key is any string, and the value is a function. I then loop through each key in the dictionary and call the functions, like so:
class SomeClass:
dictionary = {}
# Not sure how to use this decorator function
def decorator(key):
def wrapper(funct):
self.dictionary[key] = funct
return funct
return wrapper
@decorator("some_val1")
def function_1(self):
...
@decorator("some_val2")
def function_2(self):
...
@decorator("some_val3")
def function_3(self):
...
def execute_all_functions(self):
for key, _ in self.dictionary.items():
self.dictionary[key]()
if __name__ == "__main__":
sclass = SomeClass()
sclass.execute_all_functions()
So this should populate dictionary
with:
{
"some_val1": function_1(),
"some_val2": function_2(),
"some_val3": function_3()
}
I'm getting this error though
self.dictionary[key] = funct
NameError: name 'self' is not defined
How would I be able to do this. Help appreciated.