2013-05-06 59 views
0

有一個在http://effbot.org/tkinterbook/tkinter-dialog-windows.htm 一件事一個例子,我不明白:的Python:從課外訪問變量比__self其他方法__(INIT)

class Dialog(Toplevel): 

    ... 

     self.result = None 

... 

class MyDialog(Dialog): 

    def apply(self): 
     first = int(self.e1.get()) 
     second = int(self.e2.get()) 
     self.result = first, second 

d = MyDialog(root) 
print d.result 

他們通過訪問self.resultapply方法內參考d.result

我想用我自己的簡單的例子來重建這個:

class Mother(object): 
    def __init__(self): 
    self.result = None 
    def apply(self): 
    pass 

class Class(Mother): 
    def apply(self): 
    self.result = "hello" 

d = Class() 
print d.result 

print d.result輸出是沒有的,而不是「你好」

請幫助。

回答

3

您從未呼叫d.apply()result設置爲「hello」。

+1

爲了更加詳細地說明,所提出的類層次結構與「Toplevel」背後的巨大差異在於,很難說Tropvel是什麼。如果我們可以假設在調用self.apply()的'Toplevel .__ init __()'內有某個調用,我們發現它們有所不同。 – glglgl 2013-05-06 09:58:06