max(), sum(), next()

Mensanator mensanator at aol.com
Thu Sep 4 13:57:35 EDT 2008


On Sep 4, 2:05 am, Thomas Bellman <bell... at lysator.liu.se> wrote:
> Mensanator <mensana... 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'

Yes, I am in fact happy to see that behaviour.

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

So,

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

gives me an error.

As does

>>> sum([None, None, None, None, None, None])

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

Why then, doesn't

>>> sum([A for A in [None, None, None, None, None, None] if A != None])
0

give me an error?

Ok, it's not a bug.

"This behaviour is by design." - Microsoft Knowledge Base

I don't like it, but I guess I'll just have to live with it.

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

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

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

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