I am working on a large program (more than 10k lines of code).
Below is a (hopefully not over-simplified) example of a problem I sometimes face:
class MyClass1(object): def func_1(self): return self.func_2() * 2 class MyClass6(MyClass1): def func_2(self): return 10 a = MyClass6().func_1() print(a) # Prints 20
I need to use in
MyClass1 a method that is defined later on in
Using this code as is, works fine. I get quite a visible warning:
and I can add a comment so that I know what is going on in the future in case I need to debug it. However, I can't use options in my IDE like Find usages, Rename etc.
Alternatively, I can use
@abstractmethod to make it explicit that
funct_2 is defined in a child class and my IDE options would work fine:
import abc class MyClass1(metaclass=abc.ABCMeta): @abc.abstractmethod def func_2(self): return 'zzzz' def func_1(self): return self.func_2() * 2 class MyClass5(MyClass1): def func_2(self): return 10 a = MyClass5().func_1() print(a) # Prints 20
... but I think this is not the way to go. For example I get weak warnings from my IDE for classes inbetween
MyClass5 (e.g. "MyClass4 has to implement abstract method...").
What is the right way to deal with a parent class using a method that is defined in a child class?
Some extra details:
MyClass1 is never called on its own. Also
func_2() has to be defined in
MyClass6 because everything it needs is defined there as well.