use strings to call functions
Klaus Neuner
klausneuner72 at googlemail.com
Mon Feb 8 06:26:39 EST 2010
>
> A file extension is not necessarily 3 chars long.
No, of course not. But it is, if I choose to use only (self-made) file
endings that are 3 chars long. Anyway, it was just an example.
> handlers = {
> ".txt" : handle_txt,
> ".py" : handle_py,
> # etc
> }
>
That is exactly what I would like to avoid: Having to map the function
'handle_txt' to '.txt'. Firstly, because I don't want to repeat
anything and secondly, because I will one day add a new function and
forget to add its name to the dictionary. (This is not severe if there
is only one dictionary for mapping functions, but it will make life a
lot harder, if a lot of mappings of this kind are used.)
What I want is calling the string directly. In Prolog, I would use
something like:
get_file_ending(File, Ending),
Predicate =.. [Ending, File],
call(Predicate).
More information about the Python-list
mailing list