python出现super.__init__:TypeError:must be type,not classobj
2017-03-12 09:42
567 查看
本人一直是非常懒的。前段几天上传了一个50行代码写python扫雷的小程序。后来因为需要改装了python2.7版本,于是乎在python3.5下面写的程序不能运行了,报出父类的TypeError错误,网上查了查应该是新式类的缘故。网上提出的解决方案是让基类继承object,这个方法没试,不过我觉得可以解决。之所以不用这个方法的原因是我继承了python2.7中Tkinter的类,如果用这种方法作修改必须得修改源码,可能会造成不必要的麻烦。测试后发现可以直接由派生类先继承Tkinter的类再继承object。
最初我定义了MButton(Tkinter.Button)抛出TypeError错误,改为MButton(Tkinter.Button,object)就ok了。值得一提的是python中方法的解释顺序由左向右,也就是object是最后被解析的,Tkinter.Button的继承关系是->Widget->BaseWidget->...->Mixc,但是Mixc却没有继承自python中的祖宗类object,于是乎使用python的内置函数super调用基类方法会出现TypeError。ok,讲到这里,贴图。
出错时:
改错后:
红圈圈起来的就是增加的基类。至此终于在python2.7下跑起来了。还有值得大家注意的一点是python3和Python2中一些模块的改变,比如urllib2分成了两个包,printf在3中成为了函数(必须使用print(),不加括号会出错),所有的模块名变成了小写。比如我使用的tkinter在2中为Tkinter。ok,扯了不少碎碎念,第一篇CSDN文章多谢支持。
最初我定义了MButton(Tkinter.Button)抛出TypeError错误,改为MButton(Tkinter.Button,object)就ok了。值得一提的是python中方法的解释顺序由左向右,也就是object是最后被解析的,Tkinter.Button的继承关系是->Widget->BaseWidget->...->Mixc,但是Mixc却没有继承自python中的祖宗类object,于是乎使用python的内置函数super调用基类方法会出现TypeError。ok,讲到这里,贴图。
出错时:
改错后:
红圈圈起来的就是增加的基类。至此终于在python2.7下跑起来了。还有值得大家注意的一点是python3和Python2中一些模块的改变,比如urllib2分成了两个包,printf在3中成为了函数(必须使用print(),不加括号会出错),所有的模块名变成了小写。比如我使用的tkinter在2中为Tkinter。ok,扯了不少碎碎念,第一篇CSDN文章多谢支持。
相关文章推荐
- python中super出现的TypeError: must be type, not classobj 原因及解决
- python中super出现的TypeError: must be type, not classobj 原因及解决
- python中super()出现的TypeError: must be type, not classobj
- [转载]python中super出现的TypeError: must be type, not classobj 原因及解决
- python中super出现的TypeError: must be type, not classobj 原因及解决
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- super() fails with error: TypeError “argument 1 must be type, not classobj”
- python:TypeError: must be type, not classobj
- TypeError: must be type, not classobj
- python2.7 使用super关键词 报错 TypeError: must be type, not&n
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- Python 读写文件 错误TypeError: write() argument must be str, not bytes+
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- Uncaught TypeError: Super expression must either be null or a function, not undefine
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- python TypeError: must be str, not bytes错误
- Uncaught (in promise) TypeError: Super expression must either be null or a function
- python3-网络编程-TypeError: send() argument 1 must be bytes or buffer, not str异常