Turning Strings into Functions

Freddy freddycooper01 at yahoo.com
Thu Sep 25 19:16:39 EDT 2003


How do you turn a string into a callable function? I'm writing a
program which takes user input and then looks up the associated
function to run from a shelved dictionary. So, I have a dictionary
with entries like this:
myDict["Case1"] = {"desc":"This is Case1", "func1": "Handler1",
"func2" : "Handler2", "args": "ACCEPT"}

Then my program gets the string "Case 1" from the user, and needs to
assign func1 and func2 as handlers for certain events.
The argument to the handlers are sockets, and I can't know in advance
what they will be.

I need to assign the handler function before it is called. So I need
to turn the string into a function (there IS a function in this module
called Handler1() ). I don't think I can use an exec call, because
that would require me to build a string with the argument first.

Any suggestions?

Freddy




More information about the Python-list mailing list