how to get function names from the file

Larry Bates larry.bates at websafe.com
Wed Feb 15 20:02:39 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.
> # After that I would like to call functions from the tuple:
> 
> functions=(printFoo, printFOO)
> for function in functions:
>    function()
> 
> Thanks for your postings
> Petr Jakes
> 
I would do this as follows:

Create dictionary with the function names as keys and the pointer to
function definition as value:

fdict={'printFoo': printFoo, 'printFOO': printFOO}
functions=('printFoo', 'printFOO')
for function in function:
    if fdict.has_key(function: fdict[function]()
    else:
        print "No function named=%s defined" % function

-Larry Bates






More information about the Python-list mailing list