Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用字符串获取函数?_Python - Fatal编程技术网

Python 如何使用字符串获取函数?

Python 如何使用字符串获取函数?,python,Python,我想获取具有函数名字符串的函数 例如 结果应该是:函数为call使用eval(): 使用eval(): 如果您想从fetch_function()中退出(),您可以使用getattr,我认为这比eval更安全: eval("fetch_function()") class Test(object): def fetch_function(): print "Function is called" test_instance = Test() my_func = ge

我想获取具有函数名字符串的函数 例如

结果应该是:函数为call

使用
eval()

使用
eval()


如果您想从
fetch_function()
中退出
()
,您可以使用
getattr
,我认为这比
eval
更安全:

eval("fetch_function()")
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()

如果您想从
fetch_function()
中退出
()
,您可以使用
getattr
,我认为这比
eval
更安全:

eval("fetch_function()")
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()

正如前面所说的eval不安全,您可以使用dict将函数映射到字符串并调用它

class test(object):
  dict_map_func = {'fetch_f': fetch_function}
  def fetch_function():
     print "Function is call"


test.dict_map_func['fetch_f']()

正如前面所说的eval不安全,您可以使用dict将函数映射到字符串并调用它

class test(object):
  dict_map_func = {'fetch_f': fetch_function}
  def fetch_function():
     print "Function is call"


test.dict_map_func['fetch_f']()

您可以使用eval:
eval(“fetch_function()”)
这不太安全,因为它为许多黑客打开了一条道路。@Markus Meskanen:thanx作为答案,但我的问题是字符串修复“fetch_function()”,我不想在这种情况下做任何更改。在这种情况下,您的解决方案不起作用。@Herome然后您应该澄清您的问题。什么不应该改变?如果字符串必须像当前一样存在于代码中,则无法执行很多操作。我在问题中已经提到,我希望使用字符串“fetch_function()”进行提取。您可以使用eval:
eval(“fetch_function()”)
这不太安全,这为许多黑客开辟了一条道路。@Markus Meskanen:答案是thanx,但我的问题是字符串修复“fetch_function()”我不想在这种情况下做任何更改,因为你的解决方案不起作用。@Heriod,你应该澄清你的问题。什么不应该改变?如果字符串必须像当前一样存在于代码中,那么就无法完成很多工作。我在问题中已经提到,我想使用字符串“fetch_function()”进行提取。如果只需要调用该方法一次,则不需要变量:
getattr(inst,“fetch_function”)()
@rednaw:Thanx作为答案,但是我不能离开()。如果您只需要调用该方法一次,那么就不需要变量:
getattr(inst,“fetch_function”)()
@rednaw:Thanx作为您的答案,但是我不能离开()。