py2经典类,继承问题

class A():

def __init__(self):
    print('A')

class B(A):

def __init__(self):
    super(B, self).__init__()

b = B()
**在py2下执行报错了
super(B, self).__init__()
TypeError: must be type, not classobj**

如果父类A使用新式类则没有问题
class A(object):

def __init__(self):
    print('A')

为什么!!!????

阅读 2.4k
2 个回答

super说明 中写到

super(type[, object-or-type])

Note: super() only works for new-style classes.

不知道Python2与Python3的细微差别,但感觉你的继承写的有问题...

super(B, self).__init__()

你在自定义B类下继承B类干什么? 让B既当儿子还要当爸爸,违背自然法则,肯定Error.如一楼所说:"super() only works for new-style classes."

或许是我的理解错误。如有错误,请不要吝啬哦,指导我改正.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题