Tuple Comprehension ???

Roel Schroeven roel at roelschroeven.net
Tue Feb 21 13:11:07 EST 2023


Hen Hanna schreef op 21/02/2023 om 5:13:
>                  (A)   print( max( * LisX ))
>                  (B)   print( sum( * LisX ))        <------- Bad syntax !!!
>
> What's most surprising is....     (A)  is ok, and  (B) is not.
>
>             even tho'   max() and sum()  have   (basically)  the same syntax...  ( takes one arg ,  whch is a list )
>
There's an important difference in syntax.

sum() takes an iterable:

sum(iterable, /, start=0)
     Return the sum of a 'start' value (default: 0) plus an iterable of 
numbers

     When the iterable is empty, return the start value.
     This function is intended specifically for use with numeric values 
and may
     reject non-numeric types.

max() on the other hand takes either an iterable or a number of 
individual elements:

max(...)
     max(iterable, *[, default=obj, key=func]) -> value
     max(arg1, arg2, *args, *[, key=func]) -> value

     With a single iterable argument, return its biggest item. The
     default keyword-only argument specifies an object to return if
     the provided iterable is empty.
     With two or more arguments, return the largest argument.

That second form of max is why max(*some_list) works while 
sum(*some_list) doesn't.

-- 
"You can fool some of the people all the time, and all of the people some
of the time, but you cannot fool all of the people all of the time."
         -- Abraham Lincoln
"You can fool too many of the people too much of the time."
         -- James Thurber



More information about the Python-list mailing list