Question regarding indirect function calls

Alex Martelli aleax at aleax.it
Sun Apr 13 17:57:56 EDT 2003


Maxwell Hammer wrote:

> I am just starting to learn python but I love it already..
> Ok the question..
> 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

getattr(plugins, func)()

should do what you desire.


Alex





More information about the Python-list mailing list