I need to use setattr to read from a configuration file, and set an attribute of a class to a function in another module. Roughly, here's the issue.
This works:
class MyClass:
def __init__(self):
self.number_cruncher = anothermodule.some_function
# method works like this, of course;
foo = MyClass()
foo.number_cruncher(inputs)
Ok, this is trivial, but what if I want to read the name of some_function
from a configuration file? Reading the file and using setattr is also simple:
def read_ini(target):
# file reading/parsing stuff here, so we effectively do:
setattr(target, 'number_cruncher', 'anothermodule.some_function')
bar = MyClass()
read_ini(bar)
bar.number_cruncher(inputs)
This gives me a 'str' object is not callable error. If I understand correctly, it is because in the first case I'm setting the attribute as a reference to the other module, but in the second case it is simply setting the attribute as a string.
Questions: 1) is my understanding of this error correct? 2) How can I use setattr
to set the attribute to be a reference to the function rather than simply a string attribute?
I've looked at other questions and found similar questions, but nothing that seemed to address this exactly.