Python function pointer

schneck picture schneck · Feb 17, 2010 · Viewed 124k times · Source

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

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Feb 17, 2010
funcdict = {
  'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction,
    ....
}

funcdict[myvar](parameter1, parameter2)