Re: Automatically generating arithmetic operations for a subclass

2009-04-15 Thread Steven D'Aprano
On Tue, 14 Apr 2009 19:24:56 +0200, Sebastian Wiesner wrote: >> Is there a trick or Pythonic idiom to make arithmetic operations on a >> class return the same type, without having to manually specify each >> method? I'm using Python 2.5, so anything related to ABCs are not an >> option. >> >> Doe

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread norseman
Steven D'Aprano wrote: I have a subclass of int where I want all the standard arithmetic operators to return my subclass, but with no other differences: class MyInt(int): def __add__(self, other): return self.__class__(super(MyInt, self).__add__(other)) # and so on for __mul__,

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Sebastian Wiesner
> I have a subclass of int where I want all the standard arithmetic > operators to return my subclass, but with no other differences: > > class MyInt(int): > def __add__(self, other): > return self.__class__(super(MyInt, self).__add__(other)) > # and so on for __mul__, __sub__, e

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread andrew cooke
Arnaud Delobelle wrote: > "andrew cooke" writes: >> Arnaud Delobelle wrote: >>> class MyInt(int): >>> for op in binops: >>> exec binop_meth % (op, op) >>> for op in unops: >>> exec unop_meth % (op, op) >>> del op >> >> what's the "del" for? > > Without it, 'op

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Arnaud Delobelle
"andrew cooke" writes: > Arnaud Delobelle wrote: >> I do this: >> >> binops = ['add', 'sub', 'mul', 'div', 'radd', 'rsub'] # etc >> unops = ['neg', 'abs', invert'] # etc >> >> binop_meth = """ >> def __%s__(self, other): >> return type(self)(int.__%s__(self, other)) >> """ >> >> unop_meth = "

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Diez B. Roggisch
andrew cooke wrote: > Arnaud Delobelle wrote: >> I do this: >> >> binops = ['add', 'sub', 'mul', 'div', 'radd', 'rsub'] # etc >> unops = ['neg', 'abs', invert'] # etc >> >> binop_meth = """ >> def __%s__(self, other): >> return type(self)(int.__%s__(self, other)) >> """ >> >> unop_meth = """ >

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread andrew cooke
Arnaud Delobelle wrote: > I do this: > > binops = ['add', 'sub', 'mul', 'div', 'radd', 'rsub'] # etc > unops = ['neg', 'abs', invert'] # etc > > binop_meth = """ > def __%s__(self, other): > return type(self)(int.__%s__(self, other)) > """ > > unop_meth = """ > def __%s__(self): > return ty

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Gerard Flanagan
Steven D'Aprano wrote: I have a subclass of int where I want all the standard arithmetic operators to return my subclass, but with no other differences: class MyInt(int): def __add__(self, other): return self.__class__(super(MyInt, self).__add__(other)) # and so on for __mul__,

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Arnaud Delobelle
Arnaud Delobelle writes: > binops = ['add', 'sub', 'mul', 'div', 'radd', 'rsub'] # etc > unops = ['neg', 'abs', invert'] # etc Oops. There's a missing quote above. It should have been, of course: unops = ['neg', 'abs', 'invert'] # etc > > binop_meth = """ > def __%s__(self, other): > re

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Arnaud Delobelle
Steven D'Aprano writes: > I have a subclass of int where I want all the standard arithmetic > operators to return my subclass, but with no other differences: > > class MyInt(int): > def __add__(self, other): > return self.__class__(super(MyInt, self).__add__(other)) > # and so on

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Paul McGuire
On Apr 14, 4:09 am, Steven D'Aprano wrote: > I have a subclass of int where I want all the standard arithmetic > operators to return my subclass, but with no other differences: > > class MyInt(int): >     def __add__(self, other): >         return self.__class__(super(MyInt, self).__add__(other))

Automatically generating arithmetic operations for a subclass

2009-04-14 Thread Steven D'Aprano
I have a subclass of int where I want all the standard arithmetic operators to return my subclass, but with no other differences: class MyInt(int): def __add__(self, other): return self.__class__(super(MyInt, self).__add__(other)) # and so on for __mul__, __sub__, etc. My quick-