2
使用Python 2.7的新类的风格,如果我的类从Object
类继承,什么是super(ClassName, self).__init__()
的行为?我的意思是,幕后发生了什么?如果我省略它,有什么区别?什么是超级的()类继承自Object的行为和__init __()?
上面的例子:
class ClassName(object):
"""docstring for ClassName"""
def __init__(self, arg):
super(ClassName, self).__init__() ## The question above is about this super
self.arg = arg
class OtherClass(ClassName):
"""docstring for OtherClass"""
def __init__(self, arg, otherArg):
super(OtherClass, self).__init__(arg) ## The question below is about this super
self.otherArg = otherArg
如果我省略了super
,什么是发生在幕后?
谢谢。
注意,在'OtherClass'调用'super'并不安全,因为没有保证的MRO隔壁班的允许参数'__init__'(例如,因为它可能是'object')。 – chepner