how to get function names from the file

Kent Johnson kent at kentsjohnson.com
Wed Feb 15 17:53:09 EST 2006


Petr Jakes wrote:
> I have got names of functions stored in the file. For the simplicity
> expect one row only with two function names: printFoo, printFOO
> In my code I would like to define functions and then to read function
> names from the file, so the functions can be executed in the order the
> function names are stored in a file.
> 
> While trying to read the names from the file I am getting always
> "strings" and I am not able to execute them.
> 
> I would like to write my code so it will look something like:
> 
> def printFoo():
>     print "foo"
> 
> def printFOO():
>     print "FOO"
> 
> # here I would like to read the file with the function names sequences
> # and to create tuple which will contain the function names.

If the functions are in the same module as the calling code:
functions=('printFoo', 'printFOO')
for function in functions:
    globals()[function]()

If the functions are in a 'functions' module:
funcs=('printFoo', 'printFOO')
for function in funcs:
    getattr(functions, function)()

Kent



More information about the Python-list mailing list