class Test: def __sub__(self, other): return NotImplemented def __rsub__(self, other): return 1 print(Test() - Test())
I would like to make a class that does something on rsub
but does nothing on sub
.
However, the code above gives
print(Test() - Test()) ~~~~~~~^~~~~~~~ TypeError: unsupported operand type(s) for -: 'Test' and 'Test'
even though __rsub__
is defined for Test
I know the docs say
These functions are only called if the left operand does not support the corresponding operation, and the operands are of different types
But how can I do what I want?
submitted by /u/Informal-Addendum435
[link] [comments]
r/learnpython class Test: def __sub__(self, other): return NotImplemented def __rsub__(self, other): return 1 print(Test() – Test()) I would like to make a class that does something on rsub but does nothing on sub. However, the code above gives print(Test() – Test()) ~~~~~~~^~~~~~~~ TypeError: unsupported operand type(s) for -: ‘Test’ and ‘Test’ even though __rsub__ is defined for Test I know the docs say These functions are only called if the left operand does not support the corresponding operation, and the operands are of different types But how can I do what I want? submitted by /u/Informal-Addendum435 [link] [comments]
class Test: def __sub__(self, other): return NotImplemented def __rsub__(self, other): return 1 print(Test() - Test())
I would like to make a class that does something on rsub
but does nothing on sub
.
However, the code above gives
print(Test() - Test()) ~~~~~~~^~~~~~~~ TypeError: unsupported operand type(s) for -: 'Test' and 'Test'
even though __rsub__
is defined for Test
I know the docs say
These functions are only called if the left operand does not support the corresponding operation, and the operands are of different types
But how can I do what I want?
submitted by /u/Informal-Addendum435
[link] [comments]