Question regarding indirect function calls

Jack Diederich jack at performancedrivers.com
Sun Apr 13 17:16:31 EDT 2003


On Sun, Apr 13, 2003 at 04:13:21PM -0400, Maxwell Hammer wrote:
> In the program I'm trying to write I have a module called "plugins"
> containing a number of functions. I only know at runtime the name of a
> function to call from "plugins", i.e. need to call a function indirectly.
> The problem is that assigning the name of one of plugin's functions to a
> variable, I cannot then call the function. IOW:
>  func=function1
>  plugins.func() ..........will fail
>  plugins.function1() .......will pass
> 
<snipped code>

What you are actually doing is this
  func = "function1"
  plugins.func()
which doens't work in the same way this doesn't work
  plugins."function1"()

Try one of these
  getattr(plugins, "function1")()
  plugins.__dict__["function1"]()


hope that helps,

-jackdied





More information about the Python-list mailing list