By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Turning Strings into Functions

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a

"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

P: n/a
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.