Re: Operator commutativity

2011-09-21 Thread Ethan Furman
Steven D'Aprano wrote: After playing around with various combinations of C1, C2, D1 and D2, it seems to me that the rule is: If the right-hand argument is a subclass of the left-hand argument, AND also defines __radd__ directly rather than inheriting it, then its __radd__ method is called before

Re: Operator commutativity

2011-09-21 Thread Ethan Furman
Mark Dickinson wrote: On Sep 21, 2:07 am, Steven D'Aprano wrote: After playing around with various combinations of C1, C2, D1 and D2, it seems to me that the rule is: If the right-hand argument is a subclass of the left-hand argument, AND also defines __radd__ directly rather than inheriting i

Re: Operator commutativity

2011-09-21 Thread Mark Dickinson
On Sep 21, 2:07 am, Steven D'Aprano wrote: > After playing around with various combinations of C1, C2, D1 and D2, it > seems to me that the rule is: > > If the right-hand argument is a subclass of the left-hand argument, AND also > defines __radd__ directly rather than inheriting it, then its __ra

Re: Operator commutativity

2011-09-20 Thread Chris Angelico
On Wed, Sep 21, 2011 at 11:07 AM, Steven D'Aprano wrote: > If the right-hand argument is a subclass of the left-hand argument, AND also > defines __radd__ directly rather than inheriting it, then its __radd__ > method is called before the left-hand argument's __add__ method. > > which strikes me a

Re: Operator commutativity

2011-09-20 Thread Steven D'Aprano
Ethan Furman wrote: > Peter Pearson wrote: >> On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman >> wrote: >> [snip] >>> Also, if the right-hand operand is a subclass of the left-hand operand >>> then Python will try right-hand_operand.__radd__ first. >> >> I don't think it works that way for me:

Re: Operator commutativity

2011-09-20 Thread Ethan Furman
Peter Pearson wrote: On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman wrote: [snip] Also, if the right-hand operand is a subclass of the left-hand operand then Python will try right-hand_operand.__radd__ first. I don't think it works that way for me: Python 2.6.5 (r265:79063, Apr 16 2010, 13

Re: Operator commutativity

2011-09-20 Thread Peter Pearson
On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman wrote: [snip] > Also, if the right-hand operand is a subclass of the left-hand operand > then Python will try right-hand_operand.__radd__ first. I don't think it works that way for me: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) >>> class C

Re: Operator commutativity

2011-09-19 Thread Westley Martínez
On Mon, Sep 19, 2011 at 10:26:30PM -0400, Roy Smith wrote: > In article <4e77eae1$0$29978$c3e8da3$54964...@news.astraweb.com>, > Steven D'Aprano wrote: > > > Westley Mart??nez wrote: > > > > > def __radd__(self, other): > > > return self.__add__(self, other) > > > > Which, inside a class,

Re: Operator commutativity

2011-09-19 Thread Roy Smith
In article <4e77eae1$0$29978$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > Westley Martínez wrote: > > > def __radd__(self, other): > > return self.__add__(self, other) > > Which, inside a class, can be simplified to: > > __radd__ = __add__ Ooh, I could see that lead

Re: Operator commutativity

2011-09-19 Thread Steven D'Aprano
Westley Martínez wrote: > def __radd__(self, other): > return self.__add__(self, other) Which, inside a class, can be simplified to: __radd__ = __add__ -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Operator commutativity

2011-09-19 Thread Westley Martínez
On Mon, Sep 19, 2011 at 01:11:51PM +0200, Henrik Faber wrote: > Hi there, > > when I have a python class X which overloads an operator, I can use that > operator to do any operation for example with an integer > > y = X() + 123 > > however, say I want the "+" operator to be commutative. Then >

Re: Operator commutativity

2011-09-19 Thread Terry Reedy
On 9/19/2011 8:48 AM, Ethan Furman wrote: Roy Smith wrote: __radd__() only solves the problem if the left-hand operand has no __add__() method itself. Only true if the left-hand operand is so ill-behaved it doesn't check to see if it makes sense to add itself to the right-hand operand. If it

Re: Operator commutativity

2011-09-19 Thread Ethan Furman
Roy Smith wrote: In article , Henrik Faber wrote: On 19.09.2011 13:23, Paul Rudin wrote: Henrik Faber writes: How can I make this commutative? Incidentally - this isn't really about commutativity at all - the question is how can you define both left and right versions of add, irrespectiv

Re: Operator commutativity

2011-09-19 Thread Roy Smith
In article , Henrik Faber wrote: > On 19.09.2011 13:23, Paul Rudin wrote: > > Henrik Faber writes: > > > >> How can I make this commutative? > > > > Incidentally - this isn't really about commutativity at all - the > > question is how can you define both left and right versions of add, > > ir

Re: Operator commutativity

2011-09-19 Thread Henrik Faber
On 19.09.2011 13:23, Paul Rudin wrote: > Henrik Faber writes: > >> How can I make this commutative? > > Incidentally - this isn't really about commutativity at all - the > question is how can you define both left and right versions of add, > irrespective of whether they yield the same result. R

Re: Operator commutativity

2011-09-19 Thread Duncan Booth
Henrik Faber wrote: > Hi there, > > when I have a python class X which overloads an operator, I can use that > operator to do any operation for example with an integer > > y = X() + 123 > > however, say I want the "+" operator to be commutative. Then > > y = 123 + X() > > should have the sam

Re: Operator commutativity

2011-09-19 Thread Paul Rudin
Henrik Faber writes: > How can I make this commutative? Incidentally - this isn't really about commutativity at all - the question is how can you define both left and right versions of add, irrespective of whether they yield the same result. I think __radd__ is what you're after. -- http://ma

Re: Operator commutativity

2011-09-19 Thread Arnaud Delobelle
On 19 September 2011 12:11, Henrik Faber wrote: > Hi there, > > when I have a python class X which overloads an operator, I can use that > operator to do any operation for example with an integer > > y = X() + 123 > > however, say I want the "+" operator to be commutative. Then > > y = 123 + X() >

Operator commutativity

2011-09-19 Thread Henrik Faber
Hi there, when I have a python class X which overloads an operator, I can use that operator to do any operation for example with an integer y = X() + 123 however, say I want the "+" operator to be commutative. Then y = 123 + X() should have the same result. However, since it does not call __ad