The code you provided already does what you want it to do.
class A:
def __init__(self):
self.list1 = []
self.list2 = []
class B(A):
pass
b = B()
print(b.list1) # prints []
When you inherit a class, you inherit all of its methods, including __init__
. Therefore, A.__init__
is (implicitly) called when initializing B
. It's roughly equivalent to
class A:
def __init__(self):
self.list1 = []
self.list2 = []
class B(A):
def __init__(self):
super().__init__()
which means (almost) the same thing. You would never do this just to be more explicit; you would only do it if you wanted to add behavior to B.__init__
that A.__init__
shouldn't have.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…