Search the difference: Why this function defenition does'nt work?

Roman Suzi rnd at onego.ru
Sun Dec 23 04:56:48 EST 2001


On Sun, 23 Dec 2001, husam wrote:

>But when I adjust the code to this:
>
>sum=['c']
>
>it still does not work, despite the fact that sum is not an undefined
>object, right?

Probably this is what is slightly more compact approach to your
problem of making sum of numbers:

>>> f = lambda *args: reduce(lambda x, y: x+y, args, 0)
>>> f(1,2,3)
6

As for slices vs. indices, it is clear that

a[0] is not a[:0]

because a[0] is an element and a[:0] is a subsequence.

I am not sure why do you need to change fun2, but
this is what will work:

def fun2 (*args):
        sum = args and args[0] or 0
        for next in args[1:]:
                sum = sum + next
        return sum

In any case, it is impossible to make universal summing function without
specifying the zero-len case.


Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Sunday, December 23, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "When a New Yorker looks as if he's suntanned, it's probably rust." _/






More information about the Python-list mailing list