On Sun, Mar 27, 2022 at 01:33:05PM -0000, malmiteria  wrote:

> C3 linéarization might be mathematically proven to be optimal, as in, 
> it's the best way to order a graph. Sure. But why would we order the 
> inheritance tree?

How else are you going to get inheritance without a linear order? The 
interpreter can only call superclass methods one at a time, in 
some linear order. You might have a tree that looks like this in part:

    # class C(A, B): ...

    A     B
     \   /
      \ /
       C

but C.method() cannot call A.method() and B.method() *simultaneously*, 
it must call one followed by the other in *some order*.

The whole point of inheritance is that (to the degree that it is 
possible) we should not explicitly care about where the methods are 
defined, only that they are defined *somewhere* in the MRO.

If you do want to explicitly specify where the methods are defined, you 
are not using inheritance. You're just calling a function.

    # No need to inherit from B, if you don't inherit from B.
    class C(A):
        def method(self):
            # Manually call B.method.
            result = B.method(self)  # Assumes that C duck-types as B.
            do_something_with(result)

So long as your C instances duck-type as B, and B does not insist on 
actual subtyping (by checking self with isinstance(), but who does 
that?), then you don't need to inherit from B to call B methods.

If you want to manage your "inheritance" manually by specifying the 
order, then just don't use automatic inheritance, and manually specify 
the order by calling functions in whatever order you want, when you 
want, where you want.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JTCNGFZ7RZPZK27MYR4GSRRJNRDSBDNJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to