python派生类的构造函数
class baseClass():
def __init__(self,mark,name):
self.mark = mark
self.name = name
class derivedClass(baseClass):
b1 = derivedClass(name='Jibin')
print b1.name
这是我最初的代码&效果很好。
(注意:我无权访问baseClass
)
但后来我不得不将一个额外的属性rank
传递给衍生类
。所以我编辑了像这样的代码。
class baseClass():
def __init__(self,mark,name):
self.mark = mark
self.name = name
class derivedClass(baseClass):
def __init__(self,rank):
self.rank = rank
b1 = derivedClass(name='Jibin')
print b1.name
这导致了错误 __init__() 获得了意外的关键字参数 'name'
这是预期的,因为 categories
的 __init__
没有参数 <代码>名称。
我不想在实际 baseClass
的 categories
b'cos 的 __init__
中添加额外的参数 name
十个参数而不是 2(mark,name) &如果我将它们全部作为派生类的附加参数,我将弄乱其参数列表。
注意:我知道使用 baseClass.__init__(self)
或 super(drivenClass, self).__init__()
初始化 baseClass
class baseClass():
def __init__(self,mark,name):
self.mark = mark
self.name = name
class derivedClass(baseClass):
b1 = derivedClass(name='Jibin')
print b1.name
This was my code initially & it worked fine.
(Note: I don't have access to baseClass
)
But later I had to pass a additional attribute rank
to derivedClass
.So I edited the code like this.
class baseClass():
def __init__(self,mark,name):
self.mark = mark
self.name = name
class derivedClass(baseClass):
def __init__(self,rank):
self.rank = rank
b1 = derivedClass(name='Jibin')
print b1.name
This caused an error __init__() got an unexpected keyword argument 'name'
This was expected as the __init__
of derivedClass
do not have a argument name
.
I don't want to add an additional argument name
to __init__
of derivedClass
b'cos in real baseClass
has ten arguments instead of 2(mark,name) & if i give all them as additional argument to derivedClass
I will be cluttering its argument list.
Note: I am aware of initializing baseClass using baseClass.__init__(self)
or super(derivedClass, self).__init__()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许你可以尝试这样的事情
Maybe you can try something like this
此博客介绍了如何解决此类问题。解决方案是让
base
以及衍生
在其__init__
中接受一个**kwargs
参数并传递该参数到对super
的调用。This blog describes how to solve this sort of problem. The solution is to have
base
as well asderived
accept a**kwargs
argument in their__init__
and pass that to the call tosuper
.衍生类
实际上不是派生自baseClass
。要在 python 中进行子类化,您必须向类定义提供父类,因此:DerivedClass
现在继承了 BaseClass 的方法,包括__init__()
。如果您不重写方法,则在子类上调用它实际上会调用超类上定义的方法。所以,如果你想允许
DerivedClass(name='Jibin')
,你需要提供一个专门的 init():现在,你还想支持额外的关键字参数到
DerivedClass()
而无需显式添加它们。实现此目的的一种方法是将所有kwargs
分配给实例属性,因此:不过,我不建议“真正”这样做。盲目地设置属性很可能会在将来引入微妙的错误(例如在不知不觉中通过传递同名的关键字arg来替换方法)
derivedClass
is not in fact derived frombaseClass
. To subclass in python you must provide the parent class to the class definition thus:DerivedClass
now inherits the methods of BaseClass, including__init__()
. If you do not override a method, calling it on your subclass actually calls the method as defined on the superclass.So, if you want to allow
DerivedClass(name='Jibin')
, you need to provide a specialised init():Now, you also want to support additional keyword arguments to
DerivedClass()
without adding them explicitly. One way to achieve this is to assign allkwargs
to instance attributes, thus:I don't advise this 'for real' though. Blindly setting attributes is likely to introduce subtle bugs in the future (such things as unknowingly replacing a method by passing a keyword arg of the same name)
你们尝试过吗
[Python] 将基类转换为派生类
我已经测试过它,似乎它有效。另外,我认为这种方法比下面的方法要好一些,因为下面的方法不执行派生函数的 init 函数。
Have you guys tried
[Python] cast base class to derived class
I have tested it, and seems it works. Also I think this method is bit better than below one since below one does not execute init function of derived function.