max(), sum(), next()

Thomas Bellman bellman at
Thu Sep 4 19:31:20 CEST 2008

Mensanator <mensanator at> wrote:

> Ok, but I don't understand why an empty list is a valid sum
> whereas a list containing None is not.

You can't conclude the behaviour of the one from the behaviour
of the other, because the two situations have nothing at all in

>> 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.

> I'm not following that. Are you saying a query that returns no
> records doesn't have a specific field containg a Null so there
> are no Nulls to poison the sum? ...tap...tap...tap. Ok, I can see
> that,


> but you don't get 0 either.

That's because the SQL sum() has a special case for "no rows
returned".  A *different* special case than the one that taint's
the sum when encountering a NULL.  It does the equivalent of

    if len(rows_returned) == 0:
	# Special case for no rows returned
	return NULL
    total = 0
    for row in rows_returned:
	value = row[column]
	if value is NULL:
	    # Special case for encountering a NULL value
	    return NULL
	total += value
    return total

Two different special cases for the two different situations.  If
you were to remove the special case for no rows returned, you
would get zero when the SELECT statement finds no rows, but the
sum would still be tainted when a NULL value is encountered..

The definition of sum in mathematics *does* do away with that
special case.  The sum of zero terms is zero.  And the Python
sum() function follows the mathematics definition in this
respect, not the SQL definition.

You can argue that Python sum() should have special cased the
empty sequence.  It's not an illogical stance to take.  It's just
a totally different issue from encountering a non-numeric element
in the sequence.  In some cases it might actually make sense to
treat the empty sequence as an error, but just ignore non-numeric
elements (i.e, treat them as if they were zero).  And in some
cases both should be an error, and in some neither should be an

Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"You are in a twisty little passage of       !  bellman @
 standards, all conflicting."                !  Make Love -- Nicht Wahr!

More information about the Python-list mailing list