471,309 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 software developers and data experts.

Turning Strings into Functions

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
Jul 18 '05 #1
3 1460

"Freddy" <fr************@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
How do you turn a string into a callable function?


If functions are defined as module scope, as is usual, an example:
def f(*args): print args .... globals()['f'](*(1,'a'))

(1, 'a')

Or you can define your own dict mapping names to functions.

Terry J. Reedy
Jul 18 '05 #2

"Freddy" <fr************@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
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.


I see you have an answer already, so I'll give you a question.
Is there any particular reason why you are storing the name
of the function in the dictionary rather than the function itself?

i.e. myDict["Case1"] = {"desc":"This is Case1", "func1": Handler1} etc

Jul 18 '05 #3
I guess I could do that. Right now I'm creating the use cases
dictionary in a totally separate module which knows nothing about my
main module's functions. But I will try either moving the code into my
current module, or including the testcases.py into my higher level
module.
Thanks for the suggestion.

Freddy

"Richard Brodie" <R.******@rl.ac.uk> wrote in message news:<bl********@newton.cc.rl.ac.uk>...
"Freddy" <fr************@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
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.


I see you have an answer already, so I'll give you a question.
Is there any particular reason why you are storing the name
of the function in the dictionary rather than the function itself?

i.e. myDict["Case1"] = {"desc":"This is Case1", "func1": Handler1} etc

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by c_monty | last post: by
4 posts views Thread by Trying_Harder | last post: by
4 posts views Thread by John Devereux | last post: by
28 posts views Thread by hlubenow | last post: by
95 posts views Thread by hstagni | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.