Given a string - execute a function by the same name

Discussion in 'Python' started by python@bdurham.com, Apr 28, 2008.

  1. Guest

    I'm parsing a simple file and given a line's keyword, would like to call
    the equivalently named function.

    There are 3 ways I can think to do this (other than a long if/elif
    construct):

    1. eval()

    2. Convert my functions to methods and use getattr( myClass, "method" )

    3. Place all my functions in dictionary and lookup the function to be
    called

    Any suggestions on the "best" way to do this?

    Thank you,
    Malcolm
    , Apr 28, 2008
    #1
    1. Advertising

  2. Guest

    One solution may be to use globals():

    >>> globals()['foo']()


    Regards,
    Marek
    , Apr 28, 2008
    #2
    1. Advertising

  3. Matimus Guest

    On Apr 28, 9:33 am, wrote:
    > I'm parsing a simple file and given a line's keyword, would like to call
    > the equivalently named function.
    >
    > There are 3 ways I can think to do this (other than a long if/elif
    > construct):
    >
    > 1. eval()
    >
    > 2. Convert my functions to methods and use getattr( myClass, "method" )
    >
    > 3. Place all my functions in dictionary and lookup the function to be
    > called
    >
    > Any suggestions on the "best" way to do this?
    >
    > Thank you,
    > Malcolm


    You can always import yourself and use getattr on that module.

    in file "mytest.py":

    def foo():
    print "foo called"

    def call_by_name(name, *args, **kwargs):
    import mytest

    f = getattr(mytest, "foo")
    f(*args, **kwargs)

    if __name__ == "__main__":
    call_by_name('foo')



    Alternatively you can import __main__ if you are always going to be
    running directly from that file.

    Matt
    Matimus, Apr 28, 2008
    #3
  4. writes:

    > I'm parsing a simple file and given a line's keyword, would like to call
    > the equivalently named function.
    >
    > There are 3 ways I can think to do this (other than a long if/elif
    > construct):
    >
    > 1. eval()
    >
    > 2. Convert my functions to methods and use getattr( myClass, "method" )
    >
    > 3. Place all my functions in dictionary and lookup the function to be
    > called


    4. Put all my functions in a module and use getattr(module, 'function')

    --
    Arnaud
    Arnaud Delobelle, Apr 28, 2008
    #4
  5. a écrit :
    > I'm parsing a simple file and given a line's keyword, would like to call
    > the equivalently named function.
    >
    > There are 3 ways I can think to do this (other than a long if/elif
    > construct):
    >
    > 1. eval()
    >
    > 2. Convert my functions to methods and use getattr( myClass, "method" )
    >
    > 3. Place all my functions in dictionary and lookup the function to be
    > called
    >


    4. Place all your functions in a module and use getattr(the_module, "func")

    5. use globals().get("func")

    > Any suggestions on the "best" way to do this?


    #1 is the worst possible solution, and #2 doesn't make sens if you don't
    need methods.

    #4 is simple but can be problematic since these functions are not
    necessarily related enough to make for a module with hi cohesion and low
    coupling.

    #5 is the simplest solution, but can be dangerous, since just any
    function in the global namespace (which includes the builtins...) can be
    called.

    #3 is a bit heaviest to setup and maintain, but much more secure since
    you explicitely choose the availables functions.

    Depending on the context (ie: where this 'simple file' comes from), I'd
    choose #3 or #5.

    My 2 cents...
    Bruno Desthuilliers, Apr 29, 2008
    #5
  6. <> wrote in message
    news:...

    > I'm parsing a simple file and given a line's keyword, would like to call
    > the equivalently named function.


    No, actually, you woudn't :) Doing so means that if your programs input
    specification ever changes, you have to rename all of the relevant
    functions. Moreover, it leaves open the possibility that you might wind up
    calling a function you didn't intend.

    The right way to solve this kind of problem is to list all the functions you
    wish to be able to call in this way, and then explicitly define a mapping
    from keywords to the appropriate functions. Which is exactly what you're
    doing in

    > 3. Place all my functions in dictionary and lookup the function to be
    > called
    Andrew Koenig, May 8, 2008
    #6
  7. Guest

    Andrew,

    > > I'm parsing a simple file and given a line's keyword, would like to call the equivalently named function.


    > No, actually, you wouldn't :) Doing so means that if your programs input specification ever changes, you have to rename all of the relevant functions. Moreover, it leaves open the possibility that you might wind up calling a function you didn't intend.


    I'm using Arnaud Delobelle's technique (see below) to insure that only
    the appropriate methods are called. A great, practical example of a
    decorator.

    <snip>
    functions = {}

    def register(func):
    functions[func.__name__] = func
    return func

    @register
    def foo(): print "Foo!"

    @register
    def bar(): print "Bar!"

    >>> functions

    {'foo': <function foo at 0x6f2f0>, 'bar': <function bar at 0x6f330>}
    >>> functions['bar']()

    Bar!
    </snip>

    Thanks for your feedback,

    Malcolm
    , May 8, 2008
    #7
    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. Lord0
    Replies:
    1
    Views:
    549
    Thomas Weidenfeller
    Apr 19, 2006
  2. Andrzej
    Replies:
    7
    Views:
    413
    Andrzej
    May 23, 2005
  3. chiara
    Replies:
    6
    Views:
    456
    Barry Schwarz
    Oct 6, 2005
  4. 2Barter.net
    Replies:
    0
    Views:
    353
    2Barter.net
    Dec 13, 2006
  5. Replies:
    3
    Views:
    248
Loading...

Share This Page