class A:
def __init__(self):
print("world")
class B(A):
def __init__(self):
print("hello")
B() # output: hello
在我使用过的所有其他语言中,超级构造函数都是隐式调用的。如何在Python中调用它?我期待super(self),但这行不通。
class A:
def __init__(self):
print("world")
class B(A):
def __init__(self):
print("hello")
B() # output: hello
在我使用过的所有其他语言中,超级构造函数都是隐式调用的。如何在Python中调用它?我期待super(self),但这行不通。
当前回答
Super()在新样式类中返回类父对象:
class A(object):
def __init__(self):
print("world")
class B(A):
def __init__(self):
print("hello")
super(B, self).__init__()
B()
其他回答
再添加一个带参数的例子:
class B(A):
def __init__(self, x, y, z):
A.__init__(self, x, y)
给定一个派生类B,它需要定义变量x, y, z,以及一个超类a,它需要定义x, y,您可以调用超类a的静态方法init,并引用当前子类实例(self),然后是期望的参数列表。
与其他答案一致,有多种方法可以调用超类方法(包括构造函数),但是在Python 3中,这个过程被简化了:
Python 3
class A(object):
def __init__(self):
print("world")
class B(A):
def __init__(self):
print("hello")
super().__init__()
Python 2
在Python 2中,必须调用稍微详细一点的版本super(<包含类名>,self),这相当于文档中的super()。
class A(object):
def __init__(self):
print "world"
class B(A):
def __init__(self):
print "hello"
super(B, self).__init__()
Super()在新样式类中返回类父对象:
class A(object):
def __init__(self):
print("world")
class B(A):
def __init__(self):
print("hello")
super(B, self).__init__()
B()
我用下面的公式扩展了之前的答案:
class A(object):
def __init__(self):
print "world"
class B(A):
def __init__(self):
print "hello"
super(self.__class__, self).__init__()
B()
这样就不必在调用super时重复类名。如果您正在编写大量的类,并且想要使初始化方法中的代码独立于类名,那么它可以派上用场。
使用Python 2。X个老式的类,它是这样的:
class A:
def __init__(self):
print "world"
class B(A):
def __init__(self):
print "hello"
A.__init__(self)