相关疑难解决方法(0)

super()失败并出现错误:当父不从对象继承时,TypeError"参数1必须是type,而不是classobj"

我得到一些我无法弄清楚的错误.任何线索我的示例代码有什么问题?

class B:
    def meth(self, arg):
        print arg

class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

print C().meth(1)
Run Code Online (Sandbox Code Playgroud)

我从"超级"内置方法的帮助下得到了示例测试代码."C"级是

这是错误:

Traceback (most recent call last):
  File "./test.py", line 10, in ?
    print C().meth(1)
  File "./test.py", line 8, in meth
    super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

仅供参考,这是来自python本身的帮助(超级):

Help on class super in module __builtin__:

class super(object)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound …
Run Code Online (Sandbox Code Playgroud)

python inheritance object parent super

186
推荐指数
4
解决办法
11万
查看次数

类型错误:super() 参数 1 必须是类型,而不是 classobj

from Tkinter import *

class Application(Frame):
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.bttnClicks = 0
        self.createWidgets()

    def createWidgets(self):
        self.bttn = Button(self)
        self.bttn["text"] = "number of clicks"
        self.bttn["command"] = self.upadteClicks
        self.bttn.grid()


    def upadteClicks(self):
        self.bttnClicks += 1
        self.bttn["text"] = "number of clicks " + str(self.bttnClicks)

root = Tk()
root.title("button that do something")
root.geometry("400x200")
app = Application(root)
root.mainloop()`
Run Code Online (Sandbox Code Playgroud)

这就是错误:

super(Application, self).__init__(master)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?该代码在 python 3.XX 中运行良好,但在 python 2.XX 中则不然。

python tkinter super python-2.7

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×2

super ×2

inheritance ×1

object ×1

parent ×1

python-2.7 ×1

tkinter ×1