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

alwbtc
  • 28,057
  • 62
  • 134
  • 188
  • When using `*args` then `args` is a list of all arguments passed. – Some programmer dude Jul 17 '12 at 10:07
  • 2
    @JoachimPileborg: A tuple, to be precise. – Sven Marnach Jul 17 '12 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?](http://stackoverflow.com/q/11519787) 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. – Martijn Pieters Jul 17 '12 at 10:09
  • But it doesn't matter what the input is, it should just add arguments using + operator. – alwbtc Jul 17 '12 at 10:13
  • 2
    Well, then what's the problem with Python's built-in `sum`? That's precisely what it does. – David Cain Jul 17 '12 at 10:23
  • Why don't you tell us what kind of input you have and what kind of output you expect? For integers, sum is unbeatable, but clearly you have different input. – Martijn Pieters Jul 17 '12 at 11:09
  • @alwbtc I added another solution to my answer which does not use sum and so does what you want. But it would be helpfull if you could say what's wrong with `sum` in the first place. – sloth Jul 17 '12 at 11:35

4 Answers4

20

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.

Community
  • 1
  • 1
sloth
  • 99,095
  • 21
  • 171
  • 219
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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    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. – Henry Gomersall Jul 17 '12 at 10:52
  • 1
    You could also leave the method as it is and implement a sensible `__radd__` method in your class. – sloth Jul 17 '12 at 11:23
  • @BigYellowCactus If it's not one's own class, then it's likely to be easier to change the function (e.g. strings). – Henry Gomersall Jul 17 '12 at 12:16
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

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
-1
def sumall(*args):
   sum_ = 0
   for num in args:
       sum_ += num
   return sum_

print(sumall(1,5,7))

The output is 13.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56