以下の Python の例では、オブジェクト x はオブジェクト y を 'has-an' としています。y から x のメソッドを呼び出せるようにしたいと考えています。
@staticmethod を使用してそれを達成することはできますが、それを行うことはお勧めしません。
オブジェクトyからオブジェクトx全体を参照する方法はありますか?
class X(object):
def __init__(self):
self.count = 5
self.y = Y() #instance of Y created.
def add2(self):
self.count += 2
class Y(object):
def modify(self):
#from here, I wanna called add2 method of object(x)
x = X()
print x.count
>>> 5
x.y.modify()
print x.count
>>> # it will print 7 (x.count=7)
前もって感謝します。