How does `super` interacts with a class's `__mro__` attribute in multiple inheritance?

后端 未结 2 378
北恋
北恋 2021-01-19 18:43

Today, I read the official doc of super.
In which it mentioned multiple inheritance will be decided by the __mro__ attribute of a class.
So I did a bit

2条回答
  •  孤街浪徒
    2021-01-19 19:09

    From chapt 32 of learning python:

    each super call selects the method from a next class following it in the MRO ordering of the class of the self subject of a method call.

    so for super(cls, instance)(isinstance(instance, cls) must be True), the method is selected from the next class in instance.__class__.__mro__ starting from instance.__class__.

    for super(cls0, cls1)(issubclass(cls1, cls0) must be True), the method is selected from next class in cls1.__mro__ starting from cls0

    in both cases, if the method is not implemented by the next class in the MRO chain, the search will skip ahead until it finds a class with the method defined.

提交回复
热议问题