max(), sum(), next()

Thomas Bellman bellman at lysator.liu.se
Thu Sep 4 03:05:19 EDT 2008


Mensanator <mensanator at aol.com> wrote:

> No, but blank cells are 0 as far as Excel is concerned.
> That behaviour causes nothing but trouble and I am
> saddened to see Python emulate such nonsense.

Then you should feel glad that the Python sum() function *does*
signal an error for the closest equivalent of "blank cells" in
a list:

    >>> sum([1, 2, 3, None, 5, 6])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Summing the elements of an empty list is *not* the same thing as
summing elements of a list where one element is None.


> There are no "empty" boxes. There are only boxes with
> known quantities and those with unknown quantities.
> I hope that's not too ivory tower.

The sum() function in Python requires exactly one box.  That box
can be empty, can contain "known quantities" (numbers, presumably),
or "unknown quantities" (non-numbers, e.g., None).  But you can't
give it zero boxes, or three boxes.


I don't have a strong view of whether sum([]) should return 0 or
raise an error, but please do not mix that question up with what
a sum over empty cells or over NULL values should yield.  They
are very different questions.

As it happens, the SQL sum() function (at least in MySQL; I don't
have any other database easily available, nor any SQL standard to
read) does return NULL for a sum over the empty sequence, so you
could argue that that would be the correct behaviour for the
Python sum() function as well, but you can't argue that because a
sum *involving* a NULL value returns NULL.


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"This isn't right.  This isn't even wrong."  !  bellman @ lysator.liu.se
                         -- Wolfgang Pauli   !  Make Love -- Nicht Wahr!



More information about the Python-list mailing list