>>> def foo():
print "foo"
>>> def bar():
print "bar"
>>> func_list = ["foo","bar"]
>>> for func in func_list:
func()
TypeError: 'str' object is not callable >>> for func in func_list:
eval(func)()
foo
bar >>> for func in func_list:
locals()[func]()
foo
bar
>>> for func in func_list:
globals()[func]()
foo
bar >>> import foo >>> getattr(foo, 'bar')()
>>> class Foo:
def do_foo(self):
...
def do_bar(self):
...
>>> f = getattr(foo_instance, 'do_' + opname)
>>> f() 总结
以上就是Python中如何使用字符串调用函数与方法的示例分享的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于Python中如何使用字符串调用函数与方法的示例分享的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84649