我想获取带有函数名字符串的函数 例如。
class test(object):
def fetch_function():
print "Function is call"
#now i want to fetch function using string
"fetch_function()"
结果应该是:Function is call
最佳答案
如果您要从 fetch_function()
中保留 ()
,您可以使用 getattr
,我认为这比 eval 更安全
:
class Test(object):
def fetch_function():
print "Function is called"
test_instance = Test()
my_func = getattr(test_instance, 'fetch_function')
# now you can call my_func just like a regular function:
my_func()
关于python - 如何使用字符串获取函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17165152/