子クラス (B) のインスタンスに、その親クラス (A) のインスタンスのメソッドの 1 つを呼び出す最善の方法は何ですか? 子クラス (B) のインスタンスへの受け渡しself
は機能しているように見えますが、どういうわけかそれは効率的または適切な方法ではないようです。これについてもっと良い方法はありますか?
class A():
def __init__(self,name):
self.instanceOfB = self.B(self)
self.name = name
def hello(self):
print 'Hello '+self.name
class B():
def __init__(self,parent):
self.parent = parent
def hi(self):
self.parent.hello() ##Call parent instance's method 'hello()' here
instanceOfA = A('Bob')
instanceOfA.instanceOfB.hi()
結果は次のようになります。
Hello Bob
@classmethod
クラスAでインスタンス化された情報に依存しない場合に@staticmethod
のみ機能しますA.hello()