I'm parsing a simple file and given a line's keyword, would like to callYou are on the right track with (2), but you need not do any conversion.
the equivalently named function.
There are 3 ways I can think to do this (other than a long if/elif
construct):
1. eval()
2. Convert my functions to methods and use getattr( myClass, "method" )
3. Place all my functions in dictionary and lookup the function to be
called
Any suggestions on the "best" way to do this?
Thank you,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list
All objects that have attributes (and a function is an attribute) can
be looked up with getattr.
For instance, from a module (function getcwd from modules os):
<built-in function getcwd>>>import os
getattr(os,'getcwd')
'/home/gherron'>>getattr(os,'getcwd')()
You can use getattr with objects, classes, modules, and probably more.
Gary Herron