0

I have a python file containing multiple functions. And in another file, I have an array which contains functions name . *How can I implement array value in the format of functions *

For example : 1 file contains

def add(a,b)   
   c = a+b
      return c

2nd file contains

functin_arry = ["add"]

Then how can I call 1st file functions into 2nd file format of an array

xcen
  • 652
  • 2
  • 6
  • 22
ragav hats
  • 67
  • 6
  • I think it's not really clear what exactly you're trying to accomplish. Can you provide more details about what you want the end result to look like? – larsks Dec 05 '19 at 04:51

2 Answers2

0

You can define your first file to have all the functions, for example :

file_1.py

import numpy as np

def func_a(a,b):
    res = a + b
    print("func_a, result : ", res)
    return res

def func_b(a,b):
    res = np.sum(np.array((a,b)))
    print("func_b, result : ", res)
    return res

To import them from another file, you'd use exec(open("file_1.py").read()) (see this answer) and you'd have the contents of the second file to be :

file_2.py

exec(open("file_1.py").read())

a = 1
b = 1 

arr = [func_a,func_b]

for i in arr:
    i(a,b)

And then you can run file_2.py in a shell by :

(ipy3) sajid@DESKTOP-NDBN82B:/mnt/c/Users/sajid/Documents/misc/temp$ python file_2.py
func_a, result :  2
func_b, result :  2   
(ipy3) sajid@DESKTOP-NDBN82B:/mnt/c/Users/sajid/Documents/misc/temp$
Sajid
  • 135
  • 10
  • Superb.. By this process am able to execute the function .. But am having array format like this in a string ```arr = ['func_a','func_b']``` when i apply this format it shows ```TypeError: 'str' object is not callable``` – ragav hats Dec 05 '19 at 06:42
  • When you define your array to be `arr=['func_a','func_b']` the contents of the array are strings (as they're nested within single quotes `'func_a'` vs `func_a`). A string is not going to take any inputs as a function does and hence you have the error. On the other hand, if you use `arr = [func_a,func_b]` as I did, you're having the contents of the array be functions and not strings. They will thus accept inputs and run as per the function definition. – Sajid Dec 05 '19 at 16:37
0

You can make use of built-in function eval or just a reference to the function for accomplishing your task.

  1. Using eval

    Just define your function & supply your function name as parameter.

    def add(x,y):
        return(x+y)
    
    eval("add")(2,3)
    

    Make sure that the argument to the eval function is a string. ie, supply your function name eval as a "string" followed by parameters.

    Output would be: 5

  2. Using function reference.

    Define your function, add a reference to the function into a dictionary & invoke function from the via dict using it's key.

    def add(x,y):
        return(x+y)
    
    dynFuncCalls={"add":add}
    
    #Reference your function to a dictionary.
    
    dynFunc["add"](2,3)
    
    #Call it.
    #Here also, the output would be: 5
    

Note: Avoid using eval, as it is a "dangerous function". It allows a user to execute a arbitrary code & it may compromise your entire system. So, just don't use it. It is a bad practice.

Use the function referencing, which I've mentioned in method 2. It is plain-simple & secure. (Make sure that you'll handle the KeyError exception).

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
initpwn
  • 40
  • 6