Converting a string to a function pointer

Discussion in 'Python' started by =?ISO-8859-1?Q?H=E5kan_Persson?=, Feb 4, 2005.

  1. Hi.

    I am trying to "convert" a string into a function pointer.
    Suppose I have the following:

    from a import a
    from b import b
    from c import c

    funcString = GetFunctionAsString()

    and funcString is a string that contains either "a", "b" or "c".
    How can I simply call the correct function?
    I have tried using getattr() but I don't know what the first (object)
    argument should be in this case.

    Thanks,
    Håkan Persson
    =?ISO-8859-1?Q?H=E5kan_Persson?=, Feb 4, 2005
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?H=E5kan_Persson?=

    John Machin Guest

    On Fri, 04 Feb 2005 12:01:35 +0100, Håkan Persson <>
    wrote:

    >Hi.
    >
    >I am trying to "convert" a string into a function pointer.
    >Suppose I have the following:
    >
    >from a import a
    >from b import b
    >from c import c
    >
    >funcString = GetFunctionAsString()
    >
    >and funcString is a string that contains either "a", "b" or "c".
    >How can I simply call the correct function?
    >I have tried using getattr() but I don't know what the first (object)
    >argument should be in this case.


    Try this:

    >>> from sys import exit
    >>> globals()

    {'__builtins__': <module '__builtin__' (built-in)>, '__name__':
    '__main__', 'exit': <built-in function exit>, '__doc__': None}
    >>> afunc = globals()["exit"]


    Do you really need to use the "from X import Y" style? Consider the
    following alternative:

    >>> import sys
    >>> afunc = getattr(sys, "exit")
    John Machin, Feb 4, 2005
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?H=E5kan_Persson?=

    Steve Holden Guest

    Håkan Persson wrote:

    > Hi.
    >
    > I am trying to "convert" a string into a function pointer.
    > Suppose I have the following:
    >
    > from a import a
    > from b import b
    > from c import c
    >
    > funcString = GetFunctionAsString()
    >
    > and funcString is a string that contains either "a", "b" or "c".
    > How can I simply call the correct function?
    > I have tried using getattr() but I don't know what the first (object)
    > argument should be in this case.
    >
    > Thanks,
    > Håkan Persson
    >
    >

    Well, one really horrible way would be

    getattr(sys.modules['__main__'], funcString)

    and, of course, if you were going to use it a lot you could optimize
    slightly with

    this = sys.modules['__main__']

    followed later by

    this = getattr(this, funcString)

    Overall, however, it might be better to set uip a mapping for the
    functions you need to be accessible. This has the further merits that

    a) You can heve different, and/or multiple names for a function
    b) You can trap errors more easily
    c) erm, I forgot the third advantage, consider this the Spanish
    Inquisition in reverse :)

    So:

    funcMap = {
    "a": a,
    "A": a,
    "b": b,
    "exoticName": c }

    ...

    funcString = GetFunctionAsString()
    try:
    f = funcMap(funcString)
    except KeyError:
    print "No such function"
    raise SomethingElse
    result = f(args)

    regards
    Steve
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.pycon.org/
    Steve Holden http://www.holdenweb.com/
    Steve Holden, Feb 4, 2005
    #3
  4. =?ISO-8859-1?Q?H=E5kan_Persson?=

    Tim Roberts Guest

    Håkan Persson <> wrote:
    >
    >I am trying to "convert" a string into a function pointer.
    >Suppose I have the following:
    >
    >from a import a
    >from b import b
    >from c import c
    >
    >funcString = GetFunctionAsString()
    >
    >and funcString is a string that contains either "a", "b" or "c".
    >How can I simply call the correct function?
    >I have tried using getattr() but I don't know what the first (object)
    >argument should be in this case.


    You've received a lot of hi-tech answers, but don't overlook the simple
    (and possibly more secure) answer:

    lookup = {
    'a': a,
    'b': b,
    'c': c,
    }

    if lookup.has_key(myFunction):
    return lookup[myFunction]()
    else:
    print "Couldn't find", myFunction
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Feb 7, 2005
    #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. glen stark
    Replies:
    2
    Views:
    703
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,043
    Fraser Ross
    Aug 14, 2004
  3. murgan
    Replies:
    6
    Views:
    4,861
    Thad Smith
    Dec 21, 2005
  4. Kavya
    Replies:
    2
    Views:
    380
  5. , India
    Replies:
    8
    Views:
    953
    gwowen
    Aug 18, 2010
Loading...

Share This Page