When I subclass some class, say int
, and customise it's __add__
method and call super().__add__(other)
it returns an instance of int
, not my subclass. I could fix this by adding type(self)
before every super()
call in every method that returns an int
, but that seems excessive. There must be a better way to do this. The same thing happens with floats
and fractions.Fraction
s.
class A(int):
def __add__(self, other):
return super().__add__(other)
x = A()
print(type(x + 1))
Output:
<class 'int'>
Expected Output:
<class '__main__.A'>