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
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__,
> 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
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
"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 = "
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 = """
>
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
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__,
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
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
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))
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-
12 matches
Mail list logo