calling python functions using variables

Peter Otten __peter__ at web.de
Fri May 19 05:47:46 EDT 2006


creo wrote:

> i invoke an 'ls' command like this
> commands.ls()
> where commands.py is a file in the same directory
> 
> what i want to do is
> commands.VARIABLE()
> where VARIABLE holds the name of the function which i want to execute
> and depends on what the user has typed

You want

getattr(commands, VARIABLE)()

Peter




More information about the Python-list mailing list