Pythonic way to sum n-th list element?

Alex Martelli aleax at aleax.it
Tue Apr 22 14:10:39 CEST 2003


Anna wrote:

> On Mon, 21 Apr 2003 16:02:27 +0000, Alex Martelli wrote:
> 
> 
>> As a bonus -- sum([]) now returns 0, while in my original design it
>> raised
>> a ValueError, just like e.g. max([]) does.  I thought raising an error
>> was best in the name of "errors shouldn't pass silently" and "in the face
>> of
>> ambiguity, refuse the temptation to guess".  But -- once we decree that
>> "sum is about numbers", there's no real ambiguity, so we can have the
>> little extra handiness of having sum([]) not be an error;-).
> 
> Hooray!
> 
> Sum() is a goodness! And sum([]) returns 0 is a great goodness.
> Thanks Alex.

You're welcome, but for "sum([]) returns 0" you should thank Guido
and Tim (it would raise a ValueError if I had implemented it my way;-).

[Which may help explain why I use and love a language architected by
Guido (with lots of input from Tim & many others, of course), rather
than one architected by my own self...;-)]

To be properly thankful that we're not using an Alex-designed
language, we might want to look at a simple example from the
domain-specific language BBL (Bridge-Bidding Language) which I
designed and implemented in the '80s [well before I met Python --
but I was quite familiar with Icon, REXX and other highly
readable very-high-level languages, so that's no excuse;-)].

Here's the BBL expression to determine whether the current hand
is suitable for a weak-notrump opener...:

& (                      # conditions for a weak-notrump opener
    s ( 1 g 4  3  3  3   # shape 4333 (any), or...
        2 g 4  4  3  2   #       4432 (any), or...
        3   3- 3- 3- 5   #       5332 w 5 clubs, or...
        4   3- 3- 5  3-  #       5332 w 5 diamonds;
      )
    < ( 11                    # moreover, a 12-14 range for
        \+ SHDC c( 4 3 2 1 0) # normal Milton-Work point-count
        15
      )
  )

There -- aren't you glad I don't do language design any more?-)


Alex





More information about the Python-list mailing list