2013-05-06 60 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