6

Say for example if I have a method which takes multiple inputs like below:

def SumOf(Arg1,Arg2,Arg3):
    Sum = Arg1+Arg2+Arg3
    return sum

I have the values of Arg1, Arg2, Arg3 in a list and I want to access the method

Arguments = Arg1 + "," +  Arg2 + "," + Arg 3

I want to use the variable Arguments to call the method SumOf

SumOf(Arguments)

But I get the following error:

SumOf() takes exactly 3 arguments (1 given)

Note: The above is just an example, I need this for executing different methods based on the method name and arguments.

Please help.

2
  • SumOf function should return Sum (capital s). I'm not able to edit it. why edit requires atleast 6 letters? Commented Apr 19, 2013 at 13:56
  • What does this have to do with wxPython or webdriver? Commented Apr 19, 2013 at 17:30

2 Answers 2

11
Arguments = 1, 2, 3
SumOf(*Arguments)

(*) operator will unpack the arguments to multiple parameters.

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

Comments

5

Looks like there's quite a few issues with your code. The line...

Arguments = Arg1 + "," +  Arg2 + "," + Arg3

...suggests Arg1, Arg2 and Arg3 are strings, which you're trying to concatenate into a single, comma-separated, string.

In order for the SumOf function to work, it will need to be passed integer values, so if Arg1, Arg2 and Arg3 are strings, you'll need to convert them to integers first with the int() function, and pack them into a tuple, with something like...

Arguments = (int(Arg1), int(Arg2), int(Arg3))

...at which point you can call the function with either...

SumOf(*Arguments)

...or...

apply(SumOf, Arguments)

Additionally, you'll need to change the line...

return sum

...to...

return Sum

...otherwise you'll end up returning a reference to Python's built-in sum() function.

Comments

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.