python2.7 使用super关键词 报错 TypeError: must be type, not&n

错误试验代码:

class Base():
    def meth(self):
        print "i'm base"

class Derived(Base):
    def meth(self):
        super(Derived,self).meth()
        print "this is derived"
        
print Derived().meth()

报错原因:

super只能用于python的新类中,如果基类是经典类,则会报这个错。

新类和经典类又是什么呢?

新类:所有类都必须要有继承的类,如果什么都不想继承,就继承到object类。

经典类:什么都不用继承的类,如上面的代码就是经典类。所以报错。

 

所以修改第一行代码:



 

06/07/15 07:43:26 G:\Language study\python\Test\super.py
 1 
class Base(object):

当然了还有另外一种解决方式:
程序首行添加__metaclass__  = type

  

posted @ 2015-11-23 22:12  “人生苦短”  阅读(289)  评论(0编辑  收藏  举报