On Nov 9, 2:11 am, "Nicolas M. Thiery" <nicolas.thi...@u-psud.fr> wrote: > The main point is that writing V.sum(args) gives more information to > the system than sum(args, self.zero()): this is making a promise that > all elements in args are elements of V.
In python's call dispatch system, it does not. It says something along the lines: "Use V" to add up these arguments. I guess the convention could be that if someone calls this, then they should make sure that "args" only involves elements from V. But then V.sum(W) would not be valid in interesting cases either (apart from probably not being a finite operation). > - V.sum could be made to work for additive semigroups that don't have a zero > - V.sum could possibly optimize away one addition (which sum does not do) > - Experience tells that people just write sum(args) and forget about > the self.zero(), which leads to broken corner cases that hit hard > down the road. Yes, this is odd. python's sum really does start with adding zero, even if no initial value is given. class A(object): def __init__(self,n): self.n=n def __add__(self,b): print "called with",self,b return self.n+b.n def __radd__(b,self): print "called with",self,b return self.n+b.n def __repr__(self): return "A(%s)"%self.n sage: L=[A(1),A(2)] sage: from __builtin__ import sum sage: sum(L) called with 0 A(1) AttributeError: 'int' object has no attribute 'n' whereas reduce does the appropriate thing: sage: reduce(operator.add,L) called with A(1) A(2) 3 sage: reduce(operator.add,L,int(0)) called with 0 A(1) AttributeError: 'int' object has no attribute 'n' -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To post to this group, send email to sage-devel@googlegroups.com. To unsubscribe from this group, send email to sage-devel+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel?hl=en.