How to write a python function that adds all arguments?
Asked Answered
E

4

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

Ernestoernestus answered 17/7, 2012 at 10:5 Comment(7)
When using *args then args is a list of all arguments passed.Sodom
@JoachimPileborg: A tuple, to be precise.Laurenelaurens
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.Mussel
But it doesn't matter what the input is, it should just add arguments using + operator.Ernestoernestus
Well, then what's the problem with Python's built-in sum? That's precisely what it does.Heidt
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.Mussel
@Ernestoernestus 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.Alms
A
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.

Alms answered 17/7, 2012 at 10:6 Comment(7)
But I don't want to use sum function.Ernestoernestus
@Ernestoernestus - why don't you want to?Prompt
@Prompt perhaps he wants to use the + operator for cases other than numbers.Semeiology
@HenryGomersall In this case he should simply add the __radd__ method to his class.Alms
I assume you could reduce with operator.add instead of (lambda (x, y): x + y) in the second case, correct? (Seems correct.)Corrie
@CorporalTouchy Yes, correct. I also noticed that in my answer already.Alms
Second answer looks elegant. I'm using this in django orm for getting sum of multiple fields at one time.Soares
S
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
Sodom answered 17/7, 2012 at 10:15 Comment(3)
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.Semeiology
You could also leave the method as it is and implement a sensible __radd__ method in your class.Alms
@BigYellowCactus If it's not one's own class, then it's likely to be easier to change the function (e.g. strings).Semeiology
G
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

Goulden answered 17/7, 2012 at 10:56 Comment(0)
S
-1
def sumall(*args):
   sum_ = 0
   for num in args:
       sum_ += num
   return sum_

print(sumall(1,5,7))

The output is 13.

Steal answered 12/11, 2022 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.