schneck schneck - 1 year ago 150
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 Source
funcdict = {
  'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction,

funcdict[myvar](parameter1, parameter2)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download