8

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

7
  • When using *args then args is a list of all arguments passed. Commented Jul 17, 2012 at 10:07
  • 2
    @JoachimPileborg: A tuple, to be precise. Commented Jul 17, 2012 at 10:09
  • 1
    If this is a followup to How to find the list in a list of lists whose sum of elements is the greatest? then you need to specify what kind of input this function will take. Your question, as it now stands, will not get you any more helpful answers than what I already gave you there. Commented Jul 17, 2012 at 10:09
  • But it doesn't matter what the input is, it should just add arguments using + operator. Commented Jul 17, 2012 at 10:13
  • 2
    Well, then what's the problem with Python's built-in sum? That's precisely what it does. Commented Jul 17, 2012 at 10:23

4 Answers 4

21

Just use the sum built-in function

>>> def my_func(*args):
...     return sum(args)
...
>>> my_func(1,2,3,4)
10
>>>

Edit:

I don't know why you want to avoid sum, but here we go:

>>> def my_func(*args):
...   return reduce((lambda x, y: x + y), args)
...
>>> my_func(1,2,3,4)
10
>>>

Instead of the lambda you could also use operator.add.


Edit2:

I had a look at your other questions, and it seems your problem is using sum as the key parameter for max when using a custom class. I answered your question and provided a way to use your class with sum in my answer.

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

7 Comments

But I don't want to use sum function.
@alwbtc - why don't you want to?
@eumiro perhaps he wants to use the + operator for cases other than numbers.
@HenryGomersall In this case he should simply add the __radd__ method to his class.
I assume you could reduce with operator.add instead of (lambda (x, y): x + y) in the second case, correct? (Seems correct.)
|
6

How about this:

def my_func(*args):
    my_sum = 0
    for i in args:
        my_sum += i
    return my_sum

If you don't want to use the += operator, then

my_sum = my_sum + i

3 Comments

This is generalised to make it different to sum() by replacing my_sum = 0 with my_sum = args[0] and the loop to be for i in args[1:]. This now works on any list of objects with a sensible __add__ method.
You could also leave the method as it is and implement a sensible __radd__ method in your class.
@BigYellowCactus If it's not one's own class, then it's likely to be easier to change the function (e.g. strings).
1

If you definitely won't be using sum, then something like:

def func(*args, default=None):
    from operator import add
    try:
        return reduce(add, args)
    except TypeError as e:
        return default

or functools.reduce in Py3

Comments

-1
def sumall(*args):
   sum_ = 0
   for num in args:
       sum_ += num
   return sum_

print(sumall(1,5,7))

The output is 13.

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.