super() fails with error: TypeError “argument 1 must be type, not classobj”
来源:互联网 发布:caxa制造工程师软件 编辑:程序博客网 时间:2024/05/16 14:05
关于Python的面向对象的编程有这样一个例子:
class Bird: def __init__(self): self.hungry = 1 def eat(self): if self.hungry: print 'Aaaah...' self.hungry = 0 else: print 'No, thanks!'class SongBird(Bird): def __init__(self):# super(SongBird,self).__init__() self.sound = 'Squawk!' def sing(self): print self.sounddef test(): t=SongBird() t.sing() t.eat()if __name__ == '__main__': test()这时运行会报错,按照C++的OO特性来了解,SongBird类继承了Bird类的方法,包括魔术方法构造函数,在new出一个Songird类的对象t时,会自动执行父类的构造函数,但是在python中并不是这样,而是的借助super()这个函数来显式调用才会生效,如果没有super那个函数,会报错
加上后,同样会报错,报错信息为:
super() fails with error: TypeError “argument 1 must be type, not classobj”
参考stackoverflow的解决说明:
Your problem is that class B is not declared as a "new-style" class. Change it like so:
class B(object):
and it will work.
super()
and all subclass/superclass stuff only works with new-style classes. I recommend you get in the habit of always typing that(object)
on any class definition to make sure it is a new-style class.
Old-style classes (also known as "classic" classes) are always of type classobj
; new-style classes are of typetype
. This is why you got the error message you saw:
TypeError: super() argument 1 must be type, not classobj
1.在开头显示说明使用new-style classes,在开头加上__metaclass__=type
2.sof上的解决方法
- super() fails with error: TypeError “argument 1 must be type, not classobj”
- python出现super.__init__:TypeError:must be type,not classobj
- Python 调用super初始化报错 "super() argument 1 must be type, not classobj"
- Python 调用super初始化报错 "super() argument 1 must be type, not classobj"
- TypeError: must be type, not classobj
- python中super出现的TypeError: must be type, not classobj 原因及解决
- python2.7 使用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 原因及解决
- TypeError: argument 1 must be string or buffer, not instance
- TypeError: write() argument must be str, not bytes
- python-TypeError: write() argument must be str, not numpy.float64
- docx2txt TypeError: write() argument must be str, not bytes
- TypeError: unbound method must be called with class instance 1st argument
- python3-网络编程-TypeError: send() argument 1 must be bytes or buffer, not str异常
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- 关于TypeError: strptime() argument 1 must be str, not bytes解析
- spoj 362
- Android_观察者模式简单Demo
- android listView 自定义布局结合CheckedTextView实现多选
- hdu2371之矩阵快速幂
- 有想法但不会写代码,凭什么让技术合伙人为你的创意打工?
- super() fails with error: TypeError “argument 1 must be type, not classobj”
- L - Farm Irrigation
- hdu 2617(水)
- TA-Lib C/C++ API文档
- poj2635
- Windows核心编程之3 内核对象
- WAS节点同步
- 修改ubuntu username
- [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式