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

1
  • 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? Commented Dec 5, 2019 at 4:51

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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$ 

2 Comments

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
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.