[EMAIL PROTECTED] 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...
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to