各位前輩大家好 不好意思不知道標題這樣描述對不對 主要問題是說 如果我今天在class A裡面實體化 class B,有沒有辦法用class B去修改 class A裡的變數或用class A裡的function 例如C#的方式是像這樣: class A { B son = new B(); son.father=this } class B { A father = null; } 這樣如果A裡有個變數叫apple 就可以在class B裡面用father.apple=??? 來直接修改他的值 請問Python有類似的方法嗎? 找了蠻久的不過目前還沒看到QQ
你這樣做,class B每次都得檢查father是不是None或null..才能調用father的方法或屬性..為何不直接在B初始方法就傳入一個A實體當father?class a __init__(self): self.son = B(self)class B __init__(self,father) self.father=father