[Python] Class objects in MRO in python

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to understand Python's inheritance model. This is the example I'm using:

    class A:
    def a_m(self):
    print("A.a_m")


    class B(A):
    def b_m(self):
    print("B.b_m")


    b = B()
    print(b.__class__)
    print(b.__class__.__mro__)

    $ python cl.py

    <class '__main__.B'>
    (<class '__main__.B'>, <class '__main__.A'>, <class 'object'>)


    Based on the output, I've drawn the following diagram:

    [​IMG]

    Can you confirm it's correct?

    I'm coming from the JavaScript background, where inheritance model is a lot more cumbersome. Here's the diagram I put together to demonstrate the difference with just one class A:

    [​IMG]

    Continue reading...

Compartilhe esta Página