Converting a string to a function pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Håkan Persson

    #1

    Converting a string to a function pointer

    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 = GetFunctionAsSt ring()

    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


  • John Machin

    #2
    Re: Converting a string to a function pointer

    On Fri, 04 Feb 2005 12:01:35 +0100, Håkan Persson <hakan@zyberit. com>
    wrote:
    [color=blue]
    >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 = GetFunctionAsSt ring()
    >
    >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.[/color]

    Try this:
    [color=blue][color=green][color=darkred]
    >>> from sys import exit
    >>> globals()[/color][/color][/color]
    {'__builtins__' : <module '__builtin__' (built-in)>, '__name__':
    '__main__', 'exit': <built-in function exit>, '__doc__': None}[color=blue][color=green][color=darkred]
    >>> afunc = globals()["exit"][/color][/color][/color]

    Do you really need to use the "from X import Y" style? Consider the
    following alternative:
    [color=blue][color=green][color=darkred]
    >>> import sys
    >>> afunc = getattr(sys, "exit")[/color][/color][/color]



    Comment

    • Steve Holden

      #3
      Re: Converting a string to a function pointer

      Håkan Persson wrote:
      [color=blue]
      > 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 = GetFunctionAsSt ring()
      >
      > 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
      >
      >[/color]
      Well, one really horrible way would be

      getattr(sys.mod ules['__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 = GetFunctionAsSt ring()
      try:
      f = funcMap(funcStr ing)
      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/

      Comment

      • Tim Roberts

        #4
        Re: Converting a string to a function pointer

        Håkan Persson <hakan@zyberit. com> wrote:[color=blue]
        >
        >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 = GetFunctionAsSt ring()
        >
        >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.[/color]

        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, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        Working...