I have given a class A with methods f and g. In the implementation of A.f, the method self.g is called, i.e. A.g is used. (I am not able/willing to change this class A since it comes from a python package.)
Now I want to build a child class B of A and want to overwrite the methods f and g. In the implementation of B.f, I call super().f, i.e. A.f is used. In the implementation of B.g., I call self.f, i.e. B.f is used.
I would expect that everything is well defined:
B.g calls B.f calls A.f calls A.g
In fact, I get a RecursionError as I am running into an infinite number of recursions:
B.g calls B.f calls A.f calls B.g
What do I not understand correctly?
Here is a implementation:
class A:
def f(self):
print('Method A.f was called.')
self.g()
def g(self):
print('Method A.g was called.')
class B(A):
def f(self):
print('Method B.f was called.')
super().f()
def g(self):
print('Method B.g was called.')
self.f()
b = B()
b.g()
For clarification: In my application, A is a scikit-learn class, f is fit and g is fit_transform. In this scikit-learn class, fit_transform is explicitly overwritten, and fit unfortunately uses fit_transform. To ensure that my child class B does not inherit fit_transform from A, I have to redefine it in the usual way, and this leads to infinite recursion as explained.
self.g()is actuallyb.g()as in your initial call.