I’d like to write a python function which adds all its arguments, using + operator. Number of arguments are not specified:
def my_func(*args):
return arg1 + arg2 + arg3 + ...
How do I do it?
Best Regards
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just use the sum built-in function
Edit:
I don’t know why you want to avoid sum, but here we go:
Instead of the
lambdayou could also use operator.add.Edit2:
I had a look at your other questions, and it seems your problem is using
sumas thekeyparameter formaxwhen using a custom class. I answered your question and provided a way to use your class withsumin my answer.