Форум сайта python.su
Задача: сделать функцию, которая бы давала сумму всех аргументов, при условии, что аргументы являются числами, списками, кортежами или любой комбинацией указанных типов. Сделал так:
def add_coll(*args):
for i in args:
if isinstance(i, tuple) or isinstance(i, list):
add_coll(*i)
return sum(args)
>>> add_coll([1,2,3])
TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’Вместо
return sum(args)
print args
>>> (1, 2, 3)
>>> ([1, 2, 3],)
Офлайн
def add_coll(*args):
m = 0
for i in args:
if isinstance(i, tuple) or isinstance(i, list):
m += add_coll(*i)
else:
m += i
return m
print add_coll([1,2,3,[[1,2],2],(1,2)])
Офлайн