Re: sum() requires number, not simply __add__

2012-02-24 Thread Terry Reedy
On 2/24/2012 8:23 AM, Roy Smith wrote: In article, Antoon Pardon wrote: Python doesn't try to prevent people from shooting themselves in the foot. Yes it does! A simple example is None as a keyword to prevent assignments to it. Hmmm. Just playing around with some bizarre things to do wi

Re: sum() requires number, not simply __add__

2012-02-24 Thread Roy Smith
In article , Antoon Pardon wrote: > > Python doesn't try to prevent people from shooting themselves in the foot. > > > Yes it does! A simple example is None as a keyword to prevent > assignments to it. Hmmm. Just playing around with some bizarre things to do with None, and discovered thi

Re: sum() requires number, not simply __add__

2012-02-24 Thread Duncan Booth
Stefan Behnel wrote: > I know that you just meant this as an example, but it's worth > mentioning in this context that it's not exactly efficient to "sum up" > lists this way because there is a lot of copying involved. Each adding > of two lists creates a third one and copies all elements into it

Re: sum() requires number, not simply __add__

2012-02-24 Thread Antoon Pardon
On 02/24/2012 12:33 AM, Steven D'Aprano wrote: If your application stops working after you carelessly mess with components your application relies on, the right answer is usually: "Don't do that then." Python doesn't try to prevent people from shooting themselves in the foot. Yes it does! A

Re: sum() requires number, not simply __add__

2012-02-24 Thread Peter Otten
Buck Golemon wrote: > I feel like the design of sum() is inconsistent with other language > features of python. Often python doesn't require a specific type, only > that the type implement certain methods. > > Given a class that implements __add__ why should sum() not be able to > operate on that

Re: sum() requires number, not simply __add__

2012-02-23 Thread Chris Angelico
On Fri, Feb 24, 2012 at 10:33 AM, Steven D'Aprano wrote: > Yes, deleting _sentinel will cause the custom sum to fail, and yes, you > have missed something. > > If the caller wants to mess with your library and break it, they have > many, many ways to do so apart from deleting your private variable

Re: sum() requires number, not simply __add__

2012-02-23 Thread Steven D'Aprano
On Fri, 24 Feb 2012 08:53:49 +1100, Chris Angelico wrote: > On Fri, Feb 24, 2012 at 8:41 AM, Arnaud Delobelle > wrote: >> _sentinel = object() >> >> def sum(iterable, start=_sentinel): >>    if start is _sentinel: >> >> del _sentinel > > Somewhat off-topic: Doesn't the if statement there do a lo

Optional arguments syntax (was Re: sum() requires number, not simply __add__)

2012-02-23 Thread Chris Angelico
On Fri, Feb 24, 2012 at 9:09 AM, Arnaud Delobelle wrote: > On 23 February 2012 22:04, Chris Angelico wrote: >> On Fri, Feb 24, 2012 at 8:59 AM, Arnaud Delobelle wrote: >>> def sum(iterable, start=_sentinel, _sentinel=_sentinel): >> >> Is this a reason for Python to introduce a new syntax, such a

Re: sum() requires number, not simply __add__

2012-02-23 Thread Arnaud Delobelle
On 23 February 2012 22:04, Chris Angelico wrote: > On Fri, Feb 24, 2012 at 8:59 AM, Arnaud Delobelle wrote: >> def sum(iterable, start=_sentinel, _sentinel=_sentinel): > > Is this a reason for Python to introduce a new syntax, such as: > > def foo(blah, optional=del): >    if optional is del: pri

Re: sum() requires number, not simply __add__

2012-02-23 Thread Chris Angelico
On Fri, Feb 24, 2012 at 8:59 AM, Arnaud Delobelle wrote: > def sum(iterable, start=_sentinel, _sentinel=_sentinel): Is this a reason for Python to introduce a new syntax, such as: def foo(blah, optional=del): if optional is del: print("No argument was provided") Basically, 'del' is treated

Re: sum() requires number, not simply __add__

2012-02-23 Thread Ian Kelly
On Thu, Feb 23, 2012 at 2:53 PM, Chris Angelico wrote: > On Fri, Feb 24, 2012 at 8:41 AM, Arnaud Delobelle wrote: >> _sentinel = object() >> >> def sum(iterable, start=_sentinel): >>    if start is _sentinel: >> >> del _sentinel > > Somewhat off-topic: Doesn't the if statement there do a lookup f

Re: sum() requires number, not simply __add__

2012-02-23 Thread Arnaud Delobelle
On 23 February 2012 21:53, Chris Angelico wrote: > On Fri, Feb 24, 2012 at 8:41 AM, Arnaud Delobelle wrote: >> _sentinel = object() >> >> def sum(iterable, start=_sentinel): >>    if start is _sentinel: >> >> del _sentinel > > Somewhat off-topic: Doesn't the if statement there do a lookup for a >

Re: sum() requires number, not simply __add__

2012-02-23 Thread Ian Kelly
On Thu, Feb 23, 2012 at 2:38 PM, Buck Golemon wrote: > My proposal is still *slightly* superior in two ways: > > 1) It reduces the number of __add__ operations by one > 2) The second argument isn't strictly necessary, if you don't mind > that the 'null sum' will produce zero. It produces the wron

Re: sum() requires number, not simply __add__

2012-02-23 Thread Chris Angelico
On Fri, Feb 24, 2012 at 8:41 AM, Arnaud Delobelle wrote: > _sentinel = object() > > def sum(iterable, start=_sentinel): >    if start is _sentinel: > > del _sentinel Somewhat off-topic: Doesn't the if statement there do a lookup for a global, which would mean that 'del _sentinel' will cause it to

Re: sum() requires number, not simply __add__

2012-02-23 Thread Stefan Behnel
Chris Rebert, 23.02.2012 22:32: > On Thu, Feb 23, 2012 at 1:19 PM, Buck Golemon wrote: >> I feel like the design of sum() is inconsistent with other language >> features of python. Often python doesn't require a specific type, only >> that the type implement certain methods. >> >> Given a class th

Re: sum() requires number, not simply __add__

2012-02-23 Thread Arnaud Delobelle
On 23 February 2012 21:23, Buck Golemon wrote: > def sum(values, > base=0): >      values = > iter(values) > >      try: >          result = values.next() >      except StopIteration: >          return base > >      for value in values: >          result += value >      return result This is defi

Re: sum() requires number, not simply __add__

2012-02-23 Thread Buck Golemon
On Feb 23, 1:32 pm, Chris Rebert wrote: > On Thu, Feb 23, 2012 at 1:19 PM, Buck Golemon wrote: > > I feel like the design of sum() is inconsistent with other language > > features of python. Often python doesn't require a specific type, only > > that the type implement certain methods. > > > Give

Re: sum() requires number, not simply __add__

2012-02-23 Thread Chris Rebert
On Thu, Feb 23, 2012 at 1:19 PM, Buck Golemon wrote: > I feel like the design of sum() is inconsistent with other language > features of python. Often python doesn't require a specific type, only > that the type implement certain methods. > > Given a class that implements __add__ why should sum()

Re: sum() requires number, not simply __add__

2012-02-23 Thread Arnaud Delobelle
On 23 February 2012 21:19, Buck Golemon wrote: > I feel like the design of sum() is inconsistent with other language > features of python. Often python doesn't require a specific type, only > that the type implement certain methods. > > Given a class that implements __add__ why should sum() not be

Re: sum() requires number, not simply __add__

2012-02-23 Thread Buck Golemon
On Feb 23, 1:19 pm, Buck Golemon wrote: > I feel like the design of sum() is inconsistent with other language > features of python. Often python doesn't require a specific type, only > that the type implement certain methods. > > Given a class that implements __add__ why should sum() not be able t

sum() requires number, not simply __add__

2012-02-23 Thread Buck Golemon
I feel like the design of sum() is inconsistent with other language features of python. Often python doesn't require a specific type, only that the type implement certain methods. Given a class that implements __add__ why should sum() not be able to operate on that class? We can fix this in a bac