0

It is tricky question, I need to know one thing that...

two function with different functionality and one more function called 3rd function which will decide that to use any one function. That decision will be passed as argument. Below with clarity code.

    # 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)

Tried:

       # 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)

anyone can try this one... If above is not clear, let me know, i will try to explain with other examples.

  • What is `function_name` supposed to be? What is `function_path` supposed to be? You don't tell us. – juanpa.arrivillaga Mar 07 '22 at 18:53
  • done sir, hope its clear, basically function_name is string like "testing_function_one" and function_path is "project/testing/function_testing.py". Now i need to check in the path that testing_function_one is available or not, if its available then pass the argument to it and get the result from the funtion – hungry_in_learning Mar 07 '22 at 19:03
  • 1
    just pass the functions themselves. no need to pass a "name" and a "path" - if you got those on the calling site, you. likely also get the function: pass it. – jsbueno Mar 07 '22 at 19:13
  • Does this answer your question? [Calling a function of a module by using its name (a string)](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) – JonSG Mar 07 '22 at 20:35
  • @JonSG yeah, its works but in half, i mean global()["calling_funtion_name"]() will work only when calling_funtion_name funtion present in same file. if its in other path/file, then its not working.. as i tested. – hungry_in_learning Mar 07 '22 at 20:54
  • @jsbueno can you detail your answer or share me doct/result link from anywhere, so that i will check and test on my code. – hungry_in_learning Mar 07 '22 at 21:05

1 Answers1

0

Though possible, in Python, few times one will need to pass a function by its name as a string. Specially, if the wanted result is for the function to be called in its destination - the reason for that is that functions are themselves "first class objects" in Python, and can be assigned to new variable names (which will simply reference the function) and be passed as arguments to other functions.

So, if one wants to pass sin from the module math to be used as a numericd function inside some other code, instead of general_function_testing('sin', 'math', ...) one can simply write:

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

And the function callad with this parameter can simply use whatever name it has for the parameter to call the passed function:

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

While it is possible to retrieve a function from its name and module name as strings, its much more cumbersome due to nested packages: the code retrieveing the function would have to take care of any "."s in the "function path" as you call it, make carefull use of the built-in __import__, which allows one to import a module given its name as a string, though it has a weird API, and then retrieve the function from the module using a getattr call. And all this to have the a reference to the function object itself, which could be passed as a parameter from the very first moment.

The example above doing it via strings could be:

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)
...
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • okay, I got it but __import__ does not work when you call a function that is an indifferent directory. Eg: __import__(testing_sum) #testing_sum.py path in different dir => home/tester/app_tester/testing_sum.py. but __import__ present in home/tester/app_tester/projcet_tester/main_path.py @jsbueno, can you help me in this..??? – hungry_in_learning Mar 15 '22 at 08:35