感谢安德烈的回答——太简单了!
For those of you who care, perhaps I should have included the entire context of the problem. Here it is anyway:
In my application, users are able to write plugins in python. They must define a function with a well-defined parameter list, but I didn't want to impose any naming conventions on them.
So, as long as users write a function with the correct number of parameters and types, all they have to do is something like this (remember, this is the plugin code):
# this is my custom code - all plugins are called with a modified sys.path, so this
# imports some magic python code that defines the functions used below.
from specialPluginHelperModule import *
# define the function that does all the work in this plugin:
def mySpecialFn(paramA, paramB, paramC):
# do some work here with the parameters above:
pass
# set the above function:
setPluginFunction(mySpecialFn)
The call to setPluginFunction
takes the function object and sets it in a hidden class object (along with other plugin-configuration related stuff, this example has been simplified somewhat). When the main application wants to run the function, I use the runpy
module to run the plugin code, and then extract the class object mentioned above - this gives me the configuration data and the plugin function so I can run it cleanly (without polluting my namespace).
This entire process is repeated multiple times for different plugins over the same input, and seems to work very well for me.