2

I'm attempting to wrap a call to an operation with a variable argument list into a generalized function with fixed arguments like this:

def vectorizeIt(args, op)

op is a function with a variable number of arguments and args is a list with the arguments I'd like to pass to it. For example if len(args) == 3 then I'd like to call op like this: op(args[0],args[1],args[2]). As far as I can tell, op needs to have the arguments explicitly passed like that; I can't change the argument list to just be a single list or dictionary. Any suggestions about how to do this are appreciated.

Rich
  • 12,068
  • 9
  • 62
  • 94

1 Answers1

4

Use the "splat" operator * to apply an arbitrary number of arguments to op:

def vectorizeIt(args, op):
    op(*args)

Reference:

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677