schneck schneck - 4 months ago 14
Python Question

Python function pointer

I have a function name stored in a variable like this:

myvar = 'mypackage.mymodule.myfunction'


and I now want to call myfunction like this

myvar(parameter1, parameter2)


What's the easiest way to achieve this?

Answer
funcdict = {
  'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction,
    ....
}

funcdict[myvar](parameter1, parameter2)
Comments