Skip to content Skip to sidebar Skip to footer

Access Functions In Dictionary Part 2

As a follow-up to this question: I have 2 functions that look like this: def abc(a,b): return a+b def cde(c,d): return c+d And I want to assign it to a dictionary like th

Solution 1:

Make a seperate sequence of args and use the splat operator (*):

>>> def ab(a,b):
...   return a + b
... 
>>> def cde(c,d,e):
...   return c + d + e
... 
>>> funcs = {'ab':ab, 'cde':cde}
>>> to_call = ['ab','cde']
>>> args = [(1,2),(3,4,5)]
>>> for fs, arg in zip(to_call,args):
...   print(funcs[fs](*arg))
... 
3
12

Post a Comment for "Access Functions In Dictionary Part 2"