The short answer is that mathematical objects in Sage don't define addition by implementing __add__ and __radd__ by hand. If you want to learn about them make sure to not add Sage objects (like Sage integers). E.g. int(1) + y would work.
On Thursday, October 23, 2014 7:46:02 PM UTC+1, João Alberto Ferreira wrote: > > I am running the following Python example from the book "Learning > Python", from Mark Lutz and David Ascher, but Sage is returning a > TypeError after presenting the correct response. Can anyone explain me > why? I've found this very strange. > > sage: class Commuter: > ....: def __init__(self, val): > ....: self.val = val > ....: def __add__(self, other): > ....: print "add", self.val, other > ....: def __radd__(self, other): > ....: print "radd", self.val, other > ....: > sage: x = Commuter(88) > sage: y = Commuter(99) > sage: x + 1 > add 88 1 > sage: 1 + y > radd 99 1 > --------------------------------------------------------------------------- > > TypeError Traceback (most recent call > last) > <ipython-input-76-e047f7a3a32a> in <module>() > ----> 1 Integer(1) + y > > /usr/local/Sagemath/sage-6.3/local/lib/python2.7/site-packages/sage/structure/element.so > > > in sage.structure.element.RingElement.__add__ > (build/cythonized/sage/structure/element.c:14696)() > > /usr/local/Sagemath/sage-6.3/local/lib/python2.7/site-packages/sage/structure/coerce.so > > > in sage.structure.coerce.CoercionModel_cache_maps.bin_op > (build/cythonized/sage/structure/coerce.c:8323)() > > TypeError: unsupported operand parent(s) for '+': 'Integer Ring' and > '<type 'instance'>' > -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
