xuxiaohui9216 发表于 2018-8-4 08:33:09

python:TypeError: must be type, not classobj-YEELON

  今天遇到这个错误:
  Traceback (most recent call last):
  File &quot;t2.py&quot;, line 14, in <module>
  print Derived().meth()
  File &quot;t2.py&quot;, line 10, in meth
  super(Derived,self).meth()

  TypeError: must be type, not>  试验代码如下:
  


[*]class Base():
[*]    def meth(self):
[*]      print &quot;i'm base&quot;
[*]
[*]class Derived(Base):
[*]    def meth(self):
[*]      super(Derived,self).meth()
[*]      print &quot;this is derived&quot;
[*]
[*]print Derived().meth()
  

google了下,发现原因是:
super只能用于python的新类中,如果基类是经典类,则会报这个错。
新类和经典类又是什么呢?
新类:所有类都必须要有继承的类,如果什么都不想继承,就继承到object类。
经典类:什么都不用继承的类,如上面的代码就是经典类。所以报错。  


[*]class Base(object):
[*]    def meth(self):
[*]      print &quot;i'm base&quot;
  
页: [1]
查看完整版本: python:TypeError: must be type, not classobj-YEELON