python string to function: globals() vs sys.module vs dictionary
问题 I want to know what is the best way to map the string to a function. so far I know I can use: globals()[func_string] sys.modules[__name__] funcs_dictionary["func_string" : func] Here is the code: >>> def foo(arg): ... print "I'm foo: %s" % arg ... >>> def bar(arg): ... print "I'm bar: %s" % arg ... >>> >>> foo <function foo at 0xb742c7d4> >>> bar <function bar at 0xb742c80c> >>> globals()[func_string]: >>> def exec_funcs_globals(funcs_string): ... for func_string in funcs_string: ... func =