Can Super deal with multiple inheritance?

后端 未结 2 1991
萌比男神i
萌比男神i 2020-12-14 21:58

When inheriting from two objects like these

class Foo(object):
  def __init__(self,a):
    self.a=a

class Bar(object):
  def __init__(self,b):
    self.b=b
         


        
2条回答
  •  粉色の甜心
    2020-12-14 22:32

    Or should I not be trying to do this kind of this in the first place?

    Correct. You should be calling only one parent line of __init__s, not a tree. super will use the MRO to do a depth-first search of the parent classes looking for the proper __init__ to call. It will not and should not call all possible __init__s.

提交回复
热议问题