how to get function names from the file

Terry Reedy tjreedy at udel.edu
Wed Feb 15 18:26:48 EST 2006


"Petr Jakes" <mcbooczech at gmail.com> wrote in message 
news:1140041022.500870.141430 at g14g2000cwa.googlegroups.com...
>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"

Make a dict mapping names to functions:

funs = {'printFoo':printFoo, 'printFOO':printFOO}

> # 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:

Actually, str.split, the easiest way to separate the multiple names on a 
line, gives you a list.  Same difference to 'for'.

funnames=('printFoo', 'printFOO')
for fname in funnames:
      funs[fname]()

Terry Jan Reedy






More information about the Python-list mailing list