sum(strings)

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Jun 20 05:11:01 EDT 2003


Alex Martelli <aleax at aleax.it> wrote in 
news:w%lIa.209490$g92.4306887 at news2.tin.it:

>> which has the advantage that you can sum anything addable without
>> specifying a start value.
> 
> ...and the HUGE practical disadvantage of strongly encouraging
> a horde of newbies to rush into the worst performance trap that
> is waiting for them in Python -- putting a lot of small strings
> together into a big string with +=.  This is known as an
> "attractive nuisance" and may make you liable for the resulting
> damages;-).  No, if sum accepted a sequence of strings it WOULD
> have to delegate to ''.join in that case -- and I just couldn't
> find an elegant and totally general way to do that.  Thus, using
> sum as yet another occasion to remind newbies about the wonders
> of ''.join was deemed to be the best practical solution.
> 
I think I made the mistake of suggesting two things at once, and I'd like 
to separate them out for discussion.

The first was the question of whether sum should operate on a sequence of 
strings or throw an error. I would say that if it could be made to run as 
fast as ''.join it should, but if not the error is an understandable albeit 
somewhat grating response.

The second was my point that you need to specify a starting value for any 
non-numeric sequence. I think it would be trivial to fix that one, and I 
really don't see any drawback to it. You can take my 'simpler 
implementation' and add an explicit string check easily.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list