Re: sum and strings

2006-08-25 Thread Fredrik Lundh
Hendrik van Rooyen wrote: > | (I still think a "join" built-in would be nice, though. but anyone who > | argues that "join" should support numbers too will be whacked with a > | great big halibut.) > > Strange this - you don't *LOOK* like a Gaulish Blacksmith... no, but I have a nice safari o

Re: sum and strings

2006-08-25 Thread Paddy
Paddy wrote: <> > Why not make sum work for strings too? > > It would remove what seems like an arbitrary restriction and aid > duck-typing. If the answer is that the sum optimisations don't work for > the string datatype, then wouldn't it be better to put a trap in the > sum code diverting stri

Re: sum and strings

2006-08-25 Thread Bryan Olson
Paul Rubin wrote: > Are you saying "abc"+"def" should not be concatenation? I guess > that's reasonable. No, I'm definitely not saying that, or at least I didn't mean that. > As long as + is string concatenation though, the > principle of least astonishment suggests that "sum" should > conconca

Re: sum and strings

2006-08-25 Thread Paul Rubin
Neil Cerutti <[EMAIL PROTECTED]> writes: > So there isn't, it seems, a practical way of implementing the > sum(list of strings) -> ''.join(list of strings optimization. ''.join may not be the right way to do it, but obviously there are other ways. This isn't rocket science. -- http://mail.python

Re: sum and strings

2006-08-25 Thread Neil Cerutti
On 2006-08-25, Paul Rubin wrote: > Bryan Olson <[EMAIL PROTECTED]> writes: >> And herein is the problem: A class may implement "__add__" any >> way the programmer chooses. Python should require, or at least >> document requirements, on further properties of addition. >> Python should insist that a

Re: sum and strings

2006-08-25 Thread Paul Rubin
Bryan Olson <[EMAIL PROTECTED]> writes: > And herein is the problem: A class may implement "__add__" any > way the programmer chooses. Python should require, or at least > document requirements, on further properties of addition. Python > should insist that addition be symmetric an transitive, and

Re: sum and strings

2006-08-25 Thread Bryan Olson
Fredrik Lundh wrote: > [...] besides, in all dictionaries I've consulted, the word "sum" > means "adding numbers". That's a result of not looking deeply enough. Fredrik Lundh is partially right, in that "Sum" usually refers to addition of numbers. Nevertheless, the idea that "sum" must refer to n

Re: sum and strings

2006-08-25 Thread Hendrik van Rooyen
"Fredrik Lundh" <[EMAIL PROTECTED]> Wrote: 8< | (I still think a "join" built-in would be nice, though. but anyone who | argues that "join" should support numbers too will be whacked with a | great big halibut.) | | Strange this - you don't *LOOK* lik

Re: sum and strings

2006-08-24 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > There is an alternative: raise a warning instead of an exception. That > permits the admirable strategy of educating users that join() is > generally a better way to concatenate a large number of strings, while > still allowing programmers who want to s

Re: sum and strings

2006-08-24 Thread Steven D'Aprano
On Thu, 24 Aug 2006 18:24:19 +0200, Fredrik Lundh wrote: > besides, in all dictionaries I've consulted, the word "sum" means > "adding numbers". are you sure it wouldn't be more predictable if "sum" > converted strings to numbers ? And then on Thu, 24 Aug 2006 19:12:17 +0200 Fredrik also wrote

Re: sum and strings

2006-08-24 Thread Paul Rubin
Fredrik Lundh <[EMAIL PROTECTED]> writes: > "join" doesn't use __add__ at all, so I'm not sure in what sense that > would be more predictable. I'm probably missing something, but I > cannot think of any core method that uses a radically different > algorithm based on the *type* of one argument. 3

Re: sum and strings

2006-08-24 Thread Fredrik Lundh
Gabriel Genellina wrote: > sequences don't have to be homogeneous, and iterators cant go back. > But let GvR say that in his own words: > http://mail.python.org/pipermail/python-dev/2003-April/034854.html you could of course dispatch on the type of the second argument (the start value), but tha

Re: sum and strings

2006-08-24 Thread Gabriel Genellina
At Thursday 24/8/2006 13:12, Tim Chase wrote: The interpreter is clearly smart enough to recognize when the condition occurs such that it can throw the error...thus, why not add a few more smarts and have it simply translate it into "start+''.join(sequence)" to maintain predictable behavior acco

Re: sum and strings

2006-08-24 Thread Fredrik Lundh
Tim Chase wrote: > The interpreter is clearly smart enough to recognize when the > condition occurs such that it can throw the error...thus, why not > add a few more smarts and have it simply translate it into > "start+''.join(sequence)" to maintain predictable behavior > according to duck-typ

Re: sum and strings

2006-08-24 Thread Tim Chase
>> Just because something is slow or sub-optimal doesn't mean it >> should be an error. > > that's not an error because it would be "slow or sub-optimal" to add > custom objects, that's an error because you don't understand how "sum" > works. > > (hint: sum != reduce) No, clearly sum!=reduce.

Re: sum and strings

2006-08-23 Thread Fredrik Lundh
Tim Chase wrote: > >>> q3 = q1 + q2 > >>> q3.n, q3.i, q3.j, q3.k > (8, 13, 16, 22) > >>> sum([q1,q2]) > Traceback (most recent call last): >File "", line 1, in ? > TypeError: unsupported operand type(s) for +: 'int' and 'q' > > Just because something is slow or sub-optimal doesn't mean it

Re: sum and strings

2006-08-23 Thread Tim Chase
> Maybe he's just insisting on the principle of least surprise? > Personally, I'd rather expect sum() to work for strings and Python to > issue a warning instead of raising a type error. That warning might > also be appropriate when using sum() on other builtin sequence types. In its own way, it m

Re: sum and strings

2006-08-23 Thread neophyte
Fredrik Lundh wrote: > do you often write programs that "sums" various kinds of data types, and > for which performance issues are irrelevant? > > or are you just stuck in a "I have this idea" loop? Maybe he's just insisting on the principle of least surprise? Personally, I'd rather expect sum() t

Re: sum and strings

2006-08-21 Thread Fredrik Lundh
Steven D'Aprano wrote: >> and what exactly does the fact that Python can do operator-based >> dispatch much faster than it can do method-based dispatch have to >> do with sum's inability to add strings ? > > Sorry, I don't get you here. Are you saying that string addition doesn't > call the oper

Re: sum and strings

2006-08-21 Thread Steven D'Aprano
On Mon, 21 Aug 2006 12:55:14 +0200, Fredrik Lundh wrote: > Steven D'Aprano wrote: > >> That's a nonsense argument. There is no shortage of slow algorithms, and >> some of them are built into Python. When did O(n**2) become an error >> condition? And overhead matters: if I'm only doing a few conca

Re: sum and strings

2006-08-21 Thread Paddy
Carl Banks wrote: > > and, you know, if you really want to concatenate strings with sum, you > can > > class noopadd(object): > def __add__(self,other): > return other > > sum(["abc","def","ghi"],noopadd()) ;-) - Pad. -- http://mail.python.org/mailman/listinfo/python-list

Re: sum and strings

2006-08-21 Thread Carl Banks
Steven D'Aprano wrote: >But I think it is a shame that sum() does a > special type-check to avoid something which is only sometimes slow. It > doesn't protect against O(n**2) performance; it merely protects against > just one of an infinite number of possible "traps for the unwary". I'm not so sur

Re: sum and strings

2006-08-21 Thread Fredrik Lundh
Steven D'Aprano wrote: > That's a nonsense argument. There is no shortage of slow algorithms, and > some of them are built into Python. When did O(n**2) become an error > condition? And overhead matters: if I'm only doing a few concatenations, > it is significantly faster to add the strings using

Re: sum and strings

2006-08-21 Thread Fredrik Lundh
Alex Martelli wrote: > In terms of performance, however, the simple loop that you (rhamph) > posted is generally best -- e.g., with Python 2.5c1 on a MacbookPro: > > brain:~/downloads alex$ python -mtimeit -s'deep=[range(9)]*9' > 's=sum(deep,[])' > 10 loops, best of 3: 11.2 usec per loop > >

Re: sum and strings

2006-08-21 Thread Georg Brandl
Steven D'Aprano wrote: > On Fri, 18 Aug 2006 19:08:37 -0700, Paul Rubin wrote: > >> If the args are strings, the above is a quadratic time algorithm and >> it's better to throw an error than create such a trap for an unwary user. > > That's a nonsense argument. There is no shortage of slow algori

Re: sum and strings

2006-08-21 Thread Steven D'Aprano
On Fri, 18 Aug 2006 19:08:37 -0700, Paul Rubin wrote: > If the args are strings, the above is a quadratic time algorithm and > it's better to throw an error than create such a trap for an unwary user. That's a nonsense argument. There is no shortage of slow algorithms, and some of them are built

Re: sum and strings

2006-08-20 Thread Alex Martelli
Rhamphoryncus <[EMAIL PROTECTED]> wrote: ... > > > I believe the prefered method to flatten a list of lists is this: > > > > > > shallow = [] > > > for i in deep: > > > shallow.extend(i) > > > > > > Yes, it's three lines. It's also very easy to read. reduce() and > > > sum() are not. > > >

Re: sum and strings

2006-08-20 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Gerhard Fiedler wrote: > On 2006-08-20 07:18:44, Rhamphoryncus wrote: > >>> shallow = [] >>> [shallow.extend(i) for i in deep] >> >> I'm sure this has been mentioned before, but listcomps are for when you >> want to store the list and use it for further things, not for wh

Re: sum and strings

2006-08-20 Thread Gerhard Fiedler
On 2006-08-20 07:18:44, Rhamphoryncus wrote: >> shallow = [] >> [shallow.extend(i) for i in deep] > > I'm sure this has been mentioned before, but listcomps are for when you > want to store the list and use it for further things, not for when you > want a side effect. TOOWTDI. Can you please ex

Re: sum and strings

2006-08-20 Thread Rhamphoryncus
Paddy wrote: > Rhamphoryncus wrote: > > > > > It's worthwhile to note that the use of + as the concatenation operator > > is arbitrary. It could just have well been | or &, and has no > > relationship with mathematically addition. > > The effect of the string concatenation operator is only seconda

Re: sum and strings

2006-08-20 Thread Paddy
Rhamphoryncus wrote: > > It's worthwhile to note that the use of + as the concatenation operator > is arbitrary. It could just have well been | or &, and has no > relationship with mathematically addition. The effect of the string concatenation operator is only secondary. Secondary to the use o

Re: sum and strings

2006-08-19 Thread Carl Banks
Rhamphoryncus wrote: > It's also worth stressing (not in response to your post, but others) > that sum([[1],[2],[3]], []) is just as bad as attempting to sum > strings, both conceptually (it's not mathematical addition), and > performance-wise. Don't do it. :) Interesting. I would have guessed t

Re: sum and strings

2006-08-19 Thread Rhamphoryncus
Bill Pursell wrote: > Georg Brandl wrote: > > Paul Rubin wrote: > > > Sybren Stuvel <[EMAIL PROTECTED]> writes: > > >> Because of "there should only be one way to do it, and that way should > > >> be obvious". There are already the str.join and unicode.join methods, > > > > > > Those are obvious???

Re: sum and strings

2006-08-19 Thread Georg Brandl
Bill Pursell wrote: > Georg Brandl wrote: >> Paul Rubin wrote: >> > Sybren Stuvel <[EMAIL PROTECTED]> writes: >> >> Because of "there should only be one way to do it, and that way should >> >> be obvious". There are already the str.join and unicode.join methods, >> > >> > Those are obvious??? >> >>

Re: sum and strings

2006-08-19 Thread Bill Pursell
Georg Brandl wrote: > Paul Rubin wrote: > > Sybren Stuvel <[EMAIL PROTECTED]> writes: > >> Because of "there should only be one way to do it, and that way should > >> be obvious". There are already the str.join and unicode.join methods, > > > > Those are obvious??? > > Why would you try to sum up

Re: sum and strings

2006-08-18 Thread Georg Brandl
Paul Rubin wrote: > Georg Brandl <[EMAIL PROTECTED]> writes: >> Why would you try to sum up strings? Besides, the ''.join idiom is quite >> common in Python. > > Just because it's common doesn't mean it's obvious. In my opinion > it's as ugly as sin, and the fact that it's an idiom shows a > shor

Re: sum and strings: Summary

2006-08-18 Thread Paul Rubin
"Paddy" <[EMAIL PROTECTED]> writes: > I also found this from Guido: > http://mail.python.org/pipermail/python-dev/2003-April/034853.html > And this, in the same thread: > http://mail.python.org/pipermail/python-dev/2003-April/034854.html > > So, > The upshot is that complexity matters. The alg

Re: sum and strings: Summary

2006-08-18 Thread Paddy
Paul Rubin wrote: > "Paddy" <[EMAIL PROTECTED]> writes: > > Pythons designers seem to know and apply the advantages of having fewer > > 'themes' that can be applied with less constraints I am curious about > > such a constraint on sum. > > The opposing argument is that sum is sort of like reduce,

Re: sum and strings

2006-08-18 Thread Paul Rubin
"Paddy" <[EMAIL PROTECTED]> writes: > Pythons designers seem to know and apply the advantages of having fewer > 'themes' that can be applied with less constraints I am curious about > such a constraint on sum. The opposing argument is that sum is sort of like reduce, i.e. sum((a,b,c,d)) could conc

Re: sum and strings

2006-08-18 Thread Paul Rubin
Sybren Stuvel <[EMAIL PROTECTED]> writes: > > Those are obvious??? > Yup. Just read the string documentation and you're off. Huh? Just because some obscure weirdness like this is in the manual, doesn't make it obvious or natural. -- http://mail.python.org/mailman/listinfo/python-list

Re: sum and strings

2006-08-18 Thread Paddy
Georg Brandl wrote: > Paddy wrote: <> > > I get where you are coming from, but in this case we have a function, > > sum, that is not as geeral as it could be. sum is already here, and > > works for some types but not for strings which seems an arbitrary > > limitation that impede duck typing. > >

Re: sum and strings

2006-08-18 Thread Paul Rubin
Georg Brandl <[EMAIL PROTECTED]> writes: > Why would you try to sum up strings? Besides, the ''.join idiom is quite > common in Python. Just because it's common doesn't mean it's obvious. In my opinion it's as ugly as sin, and the fact that it's an idiom shows a shortcoming in Python. The obviou

Re: sum and strings

2006-08-18 Thread Paddy
Fredrik Lundh wrote: > Paddy wrote: > > > Here is where I see the break in the 'flow': > > > 1+2+3 > > 6 > sum([1,2,3], 0) > > 6 > [1] + [2] +[3] > > [1, 2, 3] > sum([[1],[2],[3]], []) > > [1, 2, 3] > '1' + '2' + '3' > > '123' > sum(['1','2','3'], '') > > Traceback (m

Re: sum and strings

2006-08-18 Thread Fredrik Lundh
Paddy wrote: > Here is where I see the break in the 'flow': > 1+2+3 > 6 sum([1,2,3], 0) > 6 [1] + [2] +[3] > [1, 2, 3] sum([[1],[2],[3]], []) > [1, 2, 3] '1' + '2' + '3' > '123' sum(['1','2','3'], '') > Traceback (most recent call last): > File "", line 1, in ? > T

Re: sum and strings

2006-08-18 Thread Georg Brandl
Paddy wrote: > Sybren Stuvel wrote: >> Paddy enlightened us with: >> > Well, after all the above, there is a question: >> > >> > Why not make sum work for strings too? >> >> Because of "there should only be one way to do it, and that way should >> be obvious". There are already the str.join and u

Re: sum and strings

2006-08-18 Thread Paddy
Sybren Stuvel wrote: > Paddy enlightened us with: > > Well, after all the above, there is a question: > > > > Why not make sum work for strings too? > > Because of "there should only be one way to do it, and that way should > be obvious". There are already the str.join and unicode.join methods,

Re: sum and strings

2006-08-18 Thread Paddy
Sybren Stuvel wrote: > Paddy enlightened us with: > > Well, after all the above, there is a question: > > > > Why not make sum work for strings too? > > Because of "there should only be one way to do it, and that way should > be obvious". There are already the str.join and unicode.join methods, >

Re: sum and strings

2006-08-18 Thread Steve Holden
[EMAIL PROTECTED] wrote: > Paul Rubin: > >>Sybren Stuvel: >> >>>Because of "there should only be one way to do it, and that way should >>>be obvious". There are already the str.join and unicode.join methods, >> >>Those are obvious??? > > > They aren't fully obvious (because they are methods of t

Re: sum and strings

2006-08-18 Thread bearophileHUGS
Paul Rubin: > Sybren Stuvel: > > Because of "there should only be one way to do it, and that way should > > be obvious". There are already the str.join and unicode.join methods, > > Those are obvious??? They aren't fully obvious (because they are methods of the separator string), but after reading

Re: sum and strings

2006-08-18 Thread Georg Brandl
Paul Rubin wrote: > Sybren Stuvel <[EMAIL PROTECTED]> writes: >> Because of "there should only be one way to do it, and that way should >> be obvious". There are already the str.join and unicode.join methods, > > Those are obvious??? Why would you try to sum up strings? Besides, the ''.join idio

Re: sum and strings

2006-08-18 Thread Paul Rubin
Sybren Stuvel <[EMAIL PROTECTED]> writes: > Because of "there should only be one way to do it, and that way should > be obvious". There are already the str.join and unicode.join methods, Those are obvious??? -- http://mail.python.org/mailman/listinfo/python-list

Re: sum and strings

2006-08-18 Thread Fredrik Lundh
Sybren Stuvel wrote: >> Why not make sum work for strings too? > > Because of "there should only be one way to do it, and that way should > be obvious". I would have thought that "performance" and "proper use of English" was more relevant, though. -- http://mail.python.org/mailman/listin