I am using getattr to call functions at runtime. There are many functions and they live in a few separate files. I am currently doing this the following way:
Say the functions live in PACKAGE/functions_*.py
The actual call is done in a class method:
class SomeClass(object):
...
def call(self, **kwargs):
function_name = '{}_{}'.format(self.parms['prefix'], self.parms['name'])
file_name = self.parms['prefix']
mod_name = 'PACKAGE.{}'.format(file_name)
module = __import__(mod_name, globals(), locals(), [file_name,])
return getattr(module, function_name)(**kwargs)
The above code works fine, but it appears overly complex to me, also it looks like every time this method is called it has to open the PACKAGE/function_*.py file (because there is no import statement for it).
Because this method gets executed thousands of times, I don’t want the code to be unnecessarily inefficient.
Question: Is there a better/more efficient way to do this? Or is my concern about the inefficiency of repeated file opening unfounded?
Note: It appeared cleaner to me to first import all PACKAGE.function_x.py files (there are not many), but then I could not work out how to use the getattr (module) object to refer to these imports.
Why don’t you cache the imported functions? E.g. let your class contain a
_cached_funcdictionary: