use strings to call functions

Klaus Neuner klausneuner72 at googlemail.com
Mon Feb 8 11:57:33 CET 2010


Hello,

I am writing a program that analyzes files of different formats. I
would like to use a function for each format. Obviously, functions can
be mapped to file formats. E.g. like this:

if file.endswith('xyz'):
    xyz(file)
elif file.endswith('abc'):
    abc(file)

...

Yet, I would prefer to do something of the following kind:

func = file[-3:]
apply_func(func, file)

Can something of this kind be done in Python?

Klaus




More information about the Python-list mailing list