まずあなたがスーパー機能を使用したい場合は、あなただけのpythonの唯一の新しいスタイルのプログラミングでスーパー機能をサポートするので、この
class c(object):
pass
のようなオブジェクトとして、基本クラスを使用する必要があります。
今、基本クラスの基本クラスの関数へのアクセス方法になります。あなたのケースでは、クラスCのmy_method関数をAから呼び出す方法
これは、静的で動的に2つの方法で実行できます。私は0番目のインデックスを取った理由
ここ
動的
class C(object):
def my_method(self):
print "in function c"
class B(C):
def my_method(self):
print "in function b"
class A(B):
def my_method(self):
# Call my_method from B
super(A, self).my_method()
# This is how you can call my_method from C here ?
super((self.__class__.__bases__)[0], self).my_method()
obj_a = A()
obj_a.my_method()
(self.__class__.__bases__
)はタプル型のthatsにAの基底クラスを返します。したがって、クラスBを返すので、スーパークラスの引数としてBクラスを持つと、bクラスの基本クラスのmy_method関数が返されます。
静的
class C(object):
def my_method(self):
print "in function c"
class B(C):
def my_method(self):
print "in function b"
class A(B):
def my_method(self):
# Call my_method from B
super(A, self).my_method()
# This is how you can call my_method from C here ?
obj_a = A()
super(A,obj_a).my_method() # calls function of class B
super(B,obj_a).my_method() # calls function of class A
あなたは 'SomeClass'の親クラスを意味ですか? – Dschoni
[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)のように聞こえます。一般的には、特定の祖先にアクセスする必要はありません。アクセスするユーザーを「super」にしておくだけで、それは[MRO(メソッド解決の順序)]の全般的なポイントです(https://www.python.org/download /releases/2.3/mro/)。 – MSeifert
'A()'によって作成されるオブジェクトは1つだけです。このオブジェクトはすべてのスーパークラスのインスタンスですが、親クラス用に作成された個別のオブジェクトはありません。 – kazemakase