在字符串中按函数查找并调用任何函数

find and call any function by function in string

这是一个棘手的问题,我需要知道一件事...

两个功能不同的函数和一个称为第三个函数的函数将决定使用任何一个函数。该决定将作为论据通过。下面是清晰度代码。

    # Present in project/testing/local/funtion_one.py
    def testing_function_one(par1, par2, par3): 
        """do something may be add all par value"""
        sum_parms =  par1 + par2 + par3
        return sum_params_one
    
    # Present in project/testing/local/funtion_two.py
    def testing_function_two(par1, par2, par3, par4, par5): 
        """do something may be add all par value"""
        sum_parms =  par1 + par2 + par3
        return sum_params_two
    
    # Present in project/testing/function_testing.py
    def general_function_testing(function_name, function_path, funtion_params, extra_params):
        """
        function_name: would be any function testing_function_one or testing_function_two
        function_path: path for where the function is located.
        funtion_params: arguments for that calling function.
        """
       
        Now I need like based on above params details, how to call the required function 
        using path and pass the params for that function and how to handle on passing 
        number of params for that perticular funtion.
    
        I am looking like:
        funt_res = function_name(funtion_params)
         
    
        # After getting result do something with other params.
        new_res = funt_res * extra_params


if __name__ == "__main__"
    function_name = "testing_function_two"
    function_path = "project/testing/local/funtion_two.py"
    funtion_params = pass values to testing_function_two funtion. it 
    can be {"par1": 2, "par2": 2, "par3": 4, "par4": 6, "par5": 8}
    extra_params = 50
    res = general_function_testing(function_name, function_path, 
    funtion_params, extra_params)

尝试过:

       # This part will work only when **calling_funtion_name** 
    present in same file otherwise it gives error. 
For me it should check all the project or specified path

   f_res = globals()["calling_funtion_name"](*args, **kwargs)
    print('f_ress', f_res)

任何人都可以试试这个... 如果以上不清楚,请告诉我,我会尝试用其他例子来解释。

虽然可能,但在 Python 中,很少有人需要通过名称作为字符串传递函数。特别是,如果想要的结果是函数在其目的地被调用——原因是函数本身是 Python 中的“第一个 class 对象”,并且可以分配给新的变量名(这将简单地引用该函数)并作为参数传递给其他函数。

因此,如果想从模块 math 传递 sin 以用作其他代码中的数字函数,而不是 general_function_testing('sin', 'math', ...) ,可以简单地写:

import math
general_function_testing(math.sin, ...)

带有这个参数的函数 callad 可以简单地使用参数的任何名称来调用传递的函数:

def general_function_testing(target_func, ...):
    ...
    result = target_func(argument)
    ...

虽然可能从函数的名称和模块名称中检索一个函数作为字符串,但由于嵌套的包,它要麻烦得多:检索函数的代码必须采用注意“函数路径”中的任何“.”,小心使用 built-in __import__,它允许导入一个以字符串形式命名的模块,尽管它有一个奇怪的 API,然后使用 getattr 调用从模块中检索函数。所有这一切都有一个对函数对象本身的引用,它可以从一开始就作为参数传递。

上面通过字符串执行的示例可以是:

import sys
def general_function_testing(func_name, func_path, ...):
    ...
    __import__(func_path)  # imports the module where the function lives, func_path being a string
    module = sys.modules[func_path]  # retrieves the module path itself
    target_func = getattr(module, func_name)
    result = target_func(argument)
...