[英]How to pass arguments to parent class with inheritance and with super keyword?
[英]inheritance — keyword and regular arguments in parent class
如果我做对了,那么调用ChildFoo实例的正确方法是什么? 我之所以这样问是因为我知道我应该将参数放在关键字参数之前,但是不知道在这种情况下该怎么做...
class ParentFoo(object):
def __init__(self,a,b,c=None):
pass
class ChildFoo(ParentFoo):
def __init__(self,d,e,f=None):
ParentFoo.__init__(self, a, b, c = "fing")
像这样吗
class ParentFoo(object):
def __init__(self,a,b,c=None):
print(c)
class ChildFoo(ParentFoo):
def __init__(self,d,e,f=None):
super(ChildFoo,self).__init__(d,e,c="fing")
c = ChildFoo("1","2")
官方python文档在这里https://docs.python.org/2/library/functions.html#super
由于ChildFoo
从ChildFoo
继承, ParentFoo
它还必须为父类获取参数
class ChildFoo(ParentFoo):
def __init__(self, a, b, d, e, f=None):
ParentFoo.__init__(self, a, b, c="fing")
我不添加' ChildFoo
因为似乎当ChildFoo
初始化ParentFoo
时您想要默认值。 或者,您可以为所有父参数提供默认值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.