Understood. Thank you! On Friday, October 24, 2014 2:45:46 PM UTC-2, Nils Bruin wrote: > > On Thursday, October 23, 2014 11:46:02 AM UTC-7, 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 >> > > The problem is caused by the fact that your __radd__ implementation > returns "None". If you insert a return command for a non-None value, the > example works as expected. > > Indeed Sage binary operations internally do not dispatch via __add__ and > __radd__, but __radd__ is tried as a fall-back at some point. If you call > 1+y then you're running Integer(1).__add__(y). This does not figure out a > way to do the addition. Apparently, sage chooses to not return > NotImplemented (allowing python to call __radd__), but calls __radd__ > itself and still raises an error when the return value is None. > > There may be a good reason why Sage chooses to call __radd__ manually > rather than let python do the work by returning NotImplemented, but if > there's not perhaps it would be better to stay closer to python's standard? >
-- 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 sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.