class A(object):
def init( self , name ):
super(object, self).init()
self.name = name
class B(A):
def init( self , name , birth ):
super(A , self).init( name )
self.birth = birth
我实例化A的时候,没有报错。
但是实例化B的时候,却报错: TypeError: object.init() takes no parameters
难道B里面的super方法,直接去调用object的?绕过A了?
网上搜了什么MRO啥的,但也想不通为什么这里会直接去object了。
还是我这个写法有问题?
1
zhyu 2015 年 2 月 9 日
super 用法错了,class A 里应该是 super(A, self).__init__(),class B 依此类推
|
2
Cynic222 2015 年 2 月 9 日
super(A , self).init( name ) 你要的是A的父类。。。
|
4
zerh925 2015 年 2 月 9 日
|
5
zerh925 2015 年 2 月 9 日
回复不支持markdown吗?
|
6
hahastudio 2015 年 2 月 9 日
super() 之所以 super 是因为通常你不需要知道 一个类的父类是哪些
https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods |