Trying to subclass but getting object.__init__() takes no parameters

隐身守侯 提交于 2020-01-04 03:50:48

问题


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

Now I'm getting this error object.__init__() takes no parameters:


回答1:


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()

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 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.




回答2:


Just remove your __init__ method altogether, since you aren't really doing anything there, anyway.




回答3:


The message tells you all you need to know. The super-class is object and its constructor takes no parameters. So don't pass it the parameters for your constructor since it doesn't know what to do with them.

Call it like this:

super(SyssecForm, self).__init__()


来源:https://stackoverflow.com/questions/10165405/trying-to-subclass-but-getting-object-init-takes-no-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!