Given a string - execute a function by the same name

P

python

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
 
M

Matimus

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
 
A

Arnaud Delobelle

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')
 
B

Bruno Desthuilliers

(e-mail address removed) 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...
 
A

Andrew Koenig

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
 
P

python

Andrew,
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!"
Bar!
</snip>

Thanks for your feedback,

Malcolm
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top