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
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
If you definitely won't be using
sum
, then something like:or
functools.reduce
in Py3Just use the sum built-in function
Edit:
I don't know why you want to avoid sum, but here we go:
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 thekey
parameter formax
when using a custom class. I answered your question and provided a way to use your class withsum
in my answer.How about this:
If you don't want to use the
+=
operator, then