検討:
class X:
def some_method(self):
print("X.some_method called")
class Y:
def some_method(self):
print("Y.some_method called")
class Foo(X,Y):
def some_method(self):
super().some_method()
# plus some Foo-specific work to be done here
foo_instance = Foo()
foo_instance.some_method()
出力:
X.some_method called
Foo のクラス宣言を次のように切り替えます。
class Foo(Y,X):
出力を次のように変更します。
Y.some_method called
両方の祖先メソッドを呼び出したい場合は、Foo の実装を次のように変更できます。
def some_method(self):
X().some_method()
Y().some_method()
# plus some Foo-specific work to be done here
これは私の質問につながります。コードのように明示的に実行せずに、Python にすべての祖先でメソッドを呼び出させる超秘密の方法はありますか (ここで all_ancestors キーワードを作成しています-そのようなことは実際に存在しますか?):
def some_method(self):
all_ancestors().some_method()
# plus some Foo-specific work to be done here
予想される出力は次のとおりです。
X.some_method called
Y.some_method called