尝试子类化但获取object .__ init __()不带参数 [英] Trying to subclass but getting object.__init__() takes no parameters
问题描述
我试图从webpy框架中继承web.form.Form来改变行为(它从表中呈现).我尝试过这种方式:
I'm trying to subclass web.form.Form from the webpy framework to change the behavior (it renders from in a table). I tried doing it in this way:
class SyssecForm(web.form.Form):
def __init__(self, *inputs, **kw):
super(SyssecForm, self).__init__(*inputs, **kw)
def render(self):
out='<div id="form"> '
for i in self.inputs:
html = utils.safeunicode(i.pre) + i.render() + self.rendernote(i.note) + utils.safeunicode(i.post)
out += "%s"%(html)
out += '"<div id="%s"> %s %s</div>'% (i.id, net.websafe(i.description), html)
out+= "</div>"
return out
现在我收到此错误object.__init__() takes no parameters
:
推荐答案
这对我有用(web.py 0.37):
This works for me (web.py 0.37):
import web
class SyssecForm(web.form.Form):
def __init__(self, *inputs, **kw):
super(SyssecForm, self).__init__(*inputs, **kw)
def render(self):
out='<div id="form"> '
for i in self.inputs:
html = web.utils.safeunicode(i.pre) + i.render() + self.rendernote(i.note) + web.utils.safeunicode(i.post)
out += "%s"%(html)
out += '"<div id="%s"> %s %s</div>'% (i.id, web.net.websafe(i.description), html)
out+= "</div>"
return out
form = SyssecForm(web.form.Textbox("test"))
print form.render()
您的问题是因为您可能已经过时了web.py,因为web.form.Form从object
继承了: https://github.com/webpy/webpy/commit/766709cbcae1369126a52aee4bc3bf145b5d77a8
Your problem is because you might have outdated web.py, since web.form.Form inherits from object
now: https://github.com/webpy/webpy/commit/766709cbcae1369126a52aee4bc3bf145b5d77a8
Super仅适用于新型类.您必须在类delcaration中添加object
,例如:class SyssecForm(web.form.Form, object):
,或者必须更新web.py.
Super only works for new-style classes. You have to add object
in class delcaration like this: class SyssecForm(web.form.Form, object):
or you have to update web.py.
这篇关于尝试子类化但获取object .__ init __()不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!