gpt4 book ai didi

python - super(ClassName,self).__init__() 有什么用

转载 作者:太空狗 更新时间:2023-10-30 01:08:23 55 4
gpt4 key购买 nike

<分区>

我有一个看起来像这样的类:

#!/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()

我看到一些实现建议使用 super

    def __init__(self, x):
super(Foo,self).__init__()
self.x = x
def bar(self)
#etc.

我的问题是:

  1. super(Foo,self).__init__()有什么用
  2. 它与self.x=x有何不同
  3. 如何使用 super()
  4. 使上面的代码产生相同的结果

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com