Dynamically call different python functions from shared libr

Is there a way to accomplish the following?

I want to pass a string variable functionName to a tag event script and call a function from the shared script library of the same name.

ie. if functionName = “myFunction1” then call shared.myScripts.myFunction1()

I don’t want to use a lookup table or an if statement because my list of functions to choose from will be lengthy.

I know I can pass functions as an object. Is is possible for me to get the function from the script module as an object with the string variable functionName?


x= getattr(shared.myScripts,'myFunction1')() print x

Use jython’s eval() function to convert the function name (in a custom property?) to a function object, then call it with a common set of parameters. You probably want to pass at least the event object.

func = eval('shared.myScripts.'+event.source.functionName) func(event)

Sweet! That was easy!

Thanks guys!