I’m just starting out in python after years of Java, C, c++, etc.
I have a long list of files/modules that each contain one major method that I want to call dynamically. For each keyword, I have a .py file with the name get_foo and inside each get_foo.py, there is a foo method. So I want to pass in the command name “foo” and execute the method get_foo.foo()
I really don’t want to do this with an ugly if/then/else block
sections = [ "abstract", "claim", "drawing", "examiner"]
command = "claim"
What I want to something like
exec("get_" + command + "." + command)
But I really don’t know even which areas of exec/eval/etc do this.
Use the
importlibmodule to dynamically import, andgetattr()to find your function:Or, simply import all your modules and add them to a dict to map command to callable: