3

Let say I have a function

def my_func(a,b,c):
    return a+b+c

I can only call it via a string:

eval("my_func")

Assuming I have a dictionary of corresponding parameters:

parm = {'a':1,'b':2,'c':3}

How do I pass it in to eval("my_func")? I know that you can do it like:

eval("my_func")(parm['a'],parm['b'],parm['c'])

But if this is not a general solution since when the function changes (ie a function that has 2 parmeters as oppose to 3) it wont work.

Any ideas?

user1234440
  • 22,521
  • 18
  • 61
  • 103

1 Answers1

4

As others have commented, this is what you want:

>>> eval("my_func")(**parm)
6
Jim K
  • 12,824
  • 2
  • 22
  • 51