複数の子クラスに継承される基本クラスがあります。この基本クラスにはメソッド calc_func(self) があり、子クラスで同じ名前のメソッド func(self) を使用します。これは機能しますが、コードがさらに複雑になると、この「アーキテクチャ」に従うのが非常に難しくなります。
# Base class
class base():
x = 12
def calc_func(self):
for i in range(1,4):
self.y += self.func()
# neither y nor func() are defined here so
# it is hard to know where they came from
# Child class 1
class child1(base):
y = 10
def __init__(self):
pass
def func(self): # method used in base class
self.x += self.y
print self.x
return self.x
# x was not defined here so it is
# hard to know where it came from
# Child class 2
class child2(base):
y = 15
def __init__(self):
pass
def func(self): # method used in base class
self.x *= self.y
print self.x
return self.x
# x was not defined here so it is
# hard to know where it came from
test1 = child1() # Create object
test1.calc_func() # Run method inherited from base class
test2 = child2() # Create another object
test2.calc_func() # Run method inherited from base class
アイデアは、共通コードを基本クラスに抽象化することでしたが、これは正しい方法ではないようです。メソッドと属性の特定の元のクラスを参照する命名規則を使用することで、これをより理解しやすくすることができるでしょうか? たぶん、まったく別のアーキテクチャですか?どんなアドバイスでも大歓迎です。