本问题已经有最佳答案,请猛点这里访问。
我有这样一个类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/usr/bin/env python class Foo: def __init__(self, x): self.x = x def bar(self): self.bar1_out = self.x + 5 self.bar2_out = self.x + 1 return (self.bar1_out,self.bar2_out) def qux(self,myvalue = None): first, second = myvalue or self.bar() return first + 3, second + 6 def main(): """docstring for main""" f = Foo(5) mbr_out1, mbr_out2 = f.bar() print mbr_out1,"\t", mbr_out2 mqx_out1, mqx_out2 = f.qux() print mqx_out1,"\t", mqx_out2 qout1, qout2 = f.qux((1)) print qout1,"\t", qout2 if __name__ == '__main__': main() |
我看到一些实现建议使用
1 2 3 4 5 | def __init__(self, x): super(Foo,self).__init__() self.x = x def bar(self) #etc. |
我的问题是:
How does it differ from
self.x=x ?
1 2 3 4 5 6 7 8 | class Foo(object): def __init__(self, x): self.x = x class Bar(Foo): def __init__(self, x): super(Bar, self).__init__(x) self.initial_status = False |
优于在
区别在于
如果您选择以设置
在您的示例中,没有必要使用