Getting a function name from string

Cesar Härdfeldt youshouldlistento at gmail.com
Tue May 8 03:38:48 EDT 2007


" It has been my experience that, more often than not,
any time you think you want to evaluate strings, you
don't need to.

For instance, instead of passing around the name of the
function as a string:

s = "someFunction"
eval(s)()

you can pass around the function as an object:

s = someFunction  # note the lack of brackets
s()


-- 
Steven. "

I want to read a module name and a function in that module from a ini-file
and then run the function with some arguments also read from the ini-file.
Is there a way to do this, still avoiding the eval()-method?

example:

module = modelParser.getModule()
function = modelParser.getFunc()
parms = modelParser.getParms()

(modelParser just extracts string from the ini-file)

I now have 'module' and 'function' as strings and 'parms' normally as a list
of strings. I can import the module by __import__(module) but is there
another way to call:
module.function(parms) than using eval()?

Thanks for any suggestions,

Best Cesar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070508/9730dc54/attachment.html>


More information about the Python-list mailing list