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.

1 Answer 1

4

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

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

Reference:

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

1 Comment

Thanks. Seems like I can skip vectorizeIt all together!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.