[英]inheritance — keyword and regular arguments in parent class
If I am doing this right, what is the right way to call an instance of ChildFoo? 如果我做对了,那么调用ChildFoo实例的正确方法是什么? I ask this because I know that I'm supposed to place arguments before keyword arguments, but don't know what to do in this case...
我之所以这样问是因为我知道我应该将参数放在关键字参数之前,但是不知道在这种情况下该怎么做...
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")
Something like this? 像这样吗
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")
Official python doc here https://docs.python.org/2/library/functions.html#super 官方python文档在这里https://docs.python.org/2/library/functions.html#super
Since ChildFoo
inherit from ParentFoo
it must get argument also for the parent class 由于
ChildFoo
从ChildFoo
继承, ParentFoo
它还必须为父类获取参数
class ChildFoo(ParentFoo):
def __init__(self, a, b, d, e, f=None):
ParentFoo.__init__(self, a, b, c="fing")
I doesn't add 'c' - cause it seems that you want default value when ChildFoo
init ParentFoo
. 我不添加'
ChildFoo
因为似乎当ChildFoo
初始化ParentFoo
时您想要默认值。 Alternatively, you can supply default values to all the parent arguments 或者,您可以为所有父参数提供默认值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.