Turning Strings into Functions

Discussion in 'Python' started by Freddy, Sep 26, 2003.

  1. Freddy

    Freddy Guest

    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
     
    Freddy, Sep 26, 2003
    #1
    1. Advertising

  2. Freddy

    Terry Reedy Guest

    "Freddy" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Sep 26, 2003
    #2
    1. Advertising

  3. "Freddy" <> wrote in message
    news:...

    > 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
     
    Richard Brodie, Sep 26, 2003
    #3
  4. Freddy

    Freddy Guest

    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" <> wrote in message news:<bl0usu$>...
    > "Freddy" <> wrote in message
    > news:...
    >
    > > 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
     
    Freddy, Sep 26, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erik Cruz

    Turning rows into columns

    Erik Cruz, Sep 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,861
    Hermit Dave
    Sep 25, 2004
  2. Jacek Generowicz

    Turning builtin functions into methods.

    Jacek Generowicz, Dec 9, 2003, in forum: Python
    Replies:
    2
    Views:
    284
    Michael Hudson
    Dec 9, 2003
  3. Replies:
    4
    Views:
    335
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    803
    Malcolm
    Jun 24, 2006
  5. Steve Litt
    Replies:
    8
    Views:
    130
    James Britt
    Dec 31, 2005
Loading...

Share This Page