[Tutor] please help!

Gregor Lingl glingl@aon.at
Fri Jan 10 07:19:01 2003


Magnus Lycka schrieb:

>
>> geraldine lynch schrieb:
>>
>>> How do I sum all the elements of an array? ...
>>
>
> I'm assuming that "array" is a sequence (list or tuple)
> of numbers of some kind...
>
> "sum(array)" would seem like a reasonable approach,
> right? That's what I would do. You would have to
> define the sum function first, but that is fairly
> trivial.
>
> At 11:50 2003-01-10 +0100, Gregor Lingl wrote:
>
>> you can use the builtin function reduce for this ...
>
>
> But a more conventional solution would be

...

>   are confusing to many programmers.
> if one feels, that 

>        if hasattr(arg, '__len__'): 

is also confusing to many programmers, one
could try if

         if type(arg) in (list,tuple):

is intuitively a little clearer (and also
excludes inappropriate strings...) ...

Gregor

>
>
>