
本文详解 python 多重继承下 `__init__` 方法的协作式初始化:通过统一使用 `super()` 和关键字参数(`**kwargs`),让 `place`、`product` 与子类 `flat` 协同完成初始化,避免参数冲突与 mro 错误。
在 Python 中实现多重继承时,若希望子类同时初始化多个父类(如 Place 和 Product),直接调用 super(Parent, self).__init__(...) 或显式写 Parent.__init__(self, ...) 是错误且不可扩展的——前者会因传入非类型对象而报 TypeError: super() argument 1 must be type, not str,后者则破坏方法解析顺序(MRO),导致某些父类 __init__ 被跳过或重复调用。
真正可靠的方案是采用 协作式初始化(cooperative initialization):所有参与继承链的类统一使用 super().__init__(**kwargs),并将自身负责的参数声明为仅限关键字参数(keyword-only arguments),剩余参数无损传递给下一个 MRO 类。这样,初始化责任被逐级委托,最终由 object.__init__() 终止链条。
以下是重构后的完整示例:
class Place:
def __init__(self, *, country, city, **kwargs):
super().__init__(**kwargs) # 委托给 MRO 中下一个类(如 Product 或 object)
self.country = country
self.city = city
def show_location(self):
print(self.country, self.city)
class Product:
def __init__(self, *, price, currency='$', **kwargs):
super().__init__(**kwargs)
self.price = price
self.currency = currency
def show_price(self):
print(self.price, self.currency)
class Flat(Place, Product):
def __init__(self, *, street, number, **kwargs):
super().__init__(**kwargs) # 自动按 MRO 调用 Place.__init__ → Product.__init__ → object.__init__
self.street = street
self.number = number
def show_address(self):
print(self.number, self.street)
# 使用:所有参数均以关键字形式传入,清晰、安全、可扩展
myflat = Flat(
country='Mozambique',
city='Nampula',
street='Rua dos Combatantes',
number=4,
price=150000,
currency='MZN'
)
myflat.show_location() # Mozambique Nampula
myflat.show_price() # 150000 MZN
myflat.show_address() # 4 Rua dos Combatantes✅ 关键设计原则:
- 所有 __init__ 方法使用 * 强制关键字参数,明确职责边界;
- 每个类只消费自己需要的参数,其余通过 **kwargs 向上/向下传递;
- super().__init__(**kwargs) 在每个 __init__ 中仅出现一次,且位于参数处理之前(确保父类初始化不依赖当前类属性);
- 子类 Flat 不需知晓父类具体参数名,只需声明自身特有字段(street, number)并转发其余参数。
⚠️ 注意事项:
- 若无法修改原始父类(如第三方库中的 Place/Product),应创建适配器类(adaptor class)封装其接口,再让适配器参与协作初始化;
- 避免混合使用 super() 与显式父类调用(如 Product.__init__(self, ...)),这将破坏 MRO 一致性;
- 可通过 Flat.__mro__ 查看实际解析顺序:(Flat, Place, Product, object),验证初始化路径是否符合预期。
这种模式不仅是技术最佳实践,更是面向组合与可维护性的设计体现:它让继承关系可预测、可调试、可复用,真正发挥 Python 多重继承的表达力。










